【问题标题】:Laravel 5.5 read a file from the local diskLaravel 5.5 从本地磁盘读取文件
【发布时间】:2023-03-13 11:29:02
【问题描述】:

我正在尝试使用 Laravel 5.5 从本地文件位置导入 csv 文件。由于某种原因,它无法在我的计算机上找到该文件,但路径是正确的。

$fileLocation = $request->file('file')->store('csv');
$importFile = File::file(storage_path('app/' . $fileLocation));

【问题讨论】:

    标签: php laravel-5.4 laravel-5.5 laravel-request laravel-filesystem


    【解决方案1】:

    您可以为此使用Storage 外观。这是一个例子:

    if (Storage::disk($disk)->exists($filename)) {
        return Storage::disk($disk)->get($filename);
    }
    
    throw new FileNotFoundException(sprintf('File not found: %s', $filename), 404);
    

    【讨论】:

      【解决方案2】:

      如果你不想使用Storage门面,你可以使用下面的代码来获取文件

      $importFile = Illuminate\Support\Facades\File::get(storage_path('app/' . $fileLocation));
      

      但是当你使用 csv 作为存储参数存储文件时,它会保存在storage/csv 文件夹中,你只需要调用storage_path($fileLocation)

      确保您的存储路径正确,如需了解更多信息,请阅读here

      【讨论】:

      • File::get 似乎在 5.5 中不可用
      • 我这边的工程文件。它在 Laravel 5.6 中可用。
      • 我需要一个 use 关键字吗? File 的命名空间是什么,因为我在 Laravel 5.5 中使用 File::get() 时得到以下信息:“找不到 Class 'App\Console\Commands\File'”
      • 使用 Illuminate\Support\Facades\File;
      猜你喜欢
      • 1970-01-01
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      • 2016-05-17
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      相关资源
      最近更新 更多