简体   繁体   English

Laravel5-上传CSV文件并将其导入数据库找不到文件

[英]Laravel5 - upload CSV file and import the same to database arise file not found

Hello I'm trying to upload csv file and in instant next step move that data to database, but it's causing file not found exception. 您好,我正在尝试上载csv文件,并在下一步中将该数据移至数据库,但这导致未找到文件异常。 Please guide me where I'm wrong. 请指导我哪里错了。 Here is my existing code: 这是我现有的代码:

 public function upload(Request $request) {

    $path = $request->file('file')->storeAs('csv', 'sample.csv');
    Storage::setVisibility($path, 'public');

    Excel::filter('chunk')->load(storage_path($path))->chunk(250, function($results) {
      foreach ($results as $row) {
        User::create([
          'username' => $row->username,
          'contact' => $row->contact
        ]);
      }
    });

  }

I found my mistake my self, storage_path actually give the directory path of "storage" folder, but when we save them using Storage, it will stores at "storage/app" folder so we just need to add "app/" before $path, and it should be 我发现我自己的错误,storage_path实际上给出了“ storage”文件夹的目录路径,但是当我们使用Storage保存它们时,它将存储在“ storage / app”文件夹中,因此我们只需要在$ path之前添加“ app /” ,应该是

storage_path("app/" . $path)

That's works for me. 那对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM