【问题标题】:Deleting file with Laravel使用 Laravel 删除文件
【发布时间】:2018-10-17 10:53:17
【问题描述】:

我在从 Laravel 项目中删除文件时遇到问题。 该文件位于 /storage/exports 目录中,它是一个使用 Laravel Excel 库存储在磁盘上的 Excel。 这是我的代码:

$path = $excel->store('xls', false, true)['full'];
...send the xls via mail....
Storage::delete($path);

当我使用 file_exist 检查文件是否存在时,我得到了正确的结果,因此 Laravel 能够读取该文件。 我还检查了文件夹的权限,并使用 chmod 777 授予此文件夹的所有权限 有什么想法吗?

谢谢!

【问题讨论】:

  • 在使用 Laravel 之后,我学到了一件事...... EASY 变得困难了
  • 尝试使用File
  • 使用“File”门面来操作存储在 storage/app 文件夹之外的文件

标签: laravel laravel-5


【解决方案1】:

存储驱动程序已经知道根目录,因此 $path 必须是相对的,而不是完整的。因此,如果您的文件位于: /this/is/the/full/path.xls,而配置filesystems.disks.local.root 设置为/this/is/the/full,您实际上是让它在/this/is/the/full/this/is/the/full/path.xls 中查找文件。

你有两个选择。

1) 向该配置添加一个新驱动程序,并直接引用它:

'custom_location' => [
    'driver' => 'local',
    'root' => '/some/other/root/path',
]

Storage::driver('custom_location')->delete($relativePathFromRoot)

2) 一次性创建:

$rootPath = '/some/other/root/path';
$client = Storage::createLocalDriver(['root' => $rootPath]);
$client->delete($relativePathFromRoot);

【讨论】:

    【解决方案2】:

    尝试将文件存储在storage/app/exports 中。 Storage 类存储文件的默认位置是storage/app

    最好不要使用 Excel 类来存储文件,而是使用 Storage 类来保存文件:Storage::put('exports/excelfile.xls', $fileContents);

    【讨论】:

    • 谢谢。最后我使用了 Claymore 的建议,但你的想法很有帮助
    【解决方案3】:

    即使你使用的是 Laravel,你也可以使用常规的 PHP 函数。

    unlink($path);
    

    【讨论】:

      猜你喜欢
      • 2016-04-10
      • 1970-01-01
      • 2016-09-10
      • 2014-12-04
      • 2014-08-28
      • 2020-03-20
      • 2013-12-23
      • 2015-12-01
      • 1970-01-01
      相关资源
      最近更新 更多