【问题标题】:Filesystem vs UploadedFile vs Disk in LaravelLaravel 中的文件系统 vs UploadedFile vs 磁盘
【发布时间】:2017-03-10 11:29:29
【问题描述】:

我目前正在使用 Laravel 进行项目,我真的不知道如何正确处理上传的文件。

我需要将上传的文件存储到特定目录(私有,只能由服务器访问),它是一个 CSV 文件,我需要处理它的数据。所以,我打算使用fgetcsv方法逐行读取文件,但是,我需要一个文件句柄。

根据文档,上传的文件存储在临时目录中,您可以通过在请求对象上调用file('variablename') 方法来检索表示文件的UploadedFile 对象。由于我需要保留这些文件,我需要将 store 方法称为 documentation 状态(或者可能使用不同的方法)。可悲的是,store 方法返回相对于“磁盘根”的路径,这意味着在FileSystem 对象中配置的Disk 对象。可能在该路径上使用fopen 是行不通的,我需要一个绝对路径。

我想出的一个肮脏的解决方法是使用storage_path 方法,因为我知道本地磁盘上配置的根是什么 (storage_path('app'))。

应该是这样的

$local_path = $file->store('uploads', 'local');
$abspath = storage_path('app') . '/' . $local_path;
// Pass the absolute path to the business layer
// In the business layer...
$fh = fopen($abspath, 'r');
// Do the thing

有没有更好的方法?

【问题讨论】:

    标签: php laravel-5 filesystems


    【解决方案1】:

    我终于找到了如何获取绝对路径,但是该方法没有正式记录,但这对我有用:

    $abspath = Storage::disk('local')->getDriver()->getAdapter()->applyPathPrefix($local_path);
    

    【讨论】:

      猜你喜欢
      • 2015-05-02
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-03
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多