Laravel Excel自定义存储路径报错,请求技术帮助
问题:Laravel Excel 自定义存储路径报错
原本使用Laravel Excel导出CSV文件到/storage/app目录,现在需要将文件存储到自定义路径(例如下载目录),尝试指定路径后出现错误。
尝试的存储命令
$responce = Excel::store(new ClientProviderSport($compliance), $fileName, 'local', '/Downloads', \Maatwebsite\Excel\Excel::CSV);
导出类代码
protected $invoices; public function __construct(array $invoices) { $this->invoices = $invoices; } public function array(): array { return $this->invoices; } public function headings(): array { return [ 'Client ID', 'Client Name', 'Number of Provider', 'Number of Sports', 'Sports' ]; }
filesystems.php 配置片段
'links' => [ public_path('storage') => storage_path('app/public'), ],
报错信息
touch(): Unable to create file /home/user/reports/.dev/phpLaravel/storage/framework/cache/laravel-excel/laravel-excel-Hmbk3Yss26wjL4rRwfCMFfC7PiMls3PI./storage because No such file or directory
解决方法
- 修正
Excel::store参数顺序
Laravel Excel的store方法参数顺序错误,第四个参数并非自定义路径。3.x版本的正确签名为:
Excel::store($export, $path, $disk = null, $writerType = null, $withHeadings = null);
- 配置自定义磁盘(推荐方案)
在config/filesystems.php的disks数组中添加自定义磁盘配置:
'disks' => [ // 保留原有磁盘配置... 'downloads' => [ 'driver' => 'local', 'root' => '/Downloads', 'visibility' => 'public', ], ],
之后调用store时指定该磁盘:
$response = Excel::store(new ClientProviderSport($compliance), $fileName, 'downloads', \Maatwebsite\Excel\Excel::CSV);
- 直接使用绝对路径(临时测试用)
若不想配置磁盘,可将绝对路径拼接进文件名参数:
$filePath = '/Downloads/' . $fileName; $response = Excel::store(new ClientProviderSport($compliance), $filePath, 'local', \Maatwebsite\Excel\Excel::CSV);
注意:需确保/Downloads目录存在且有写入权限,执行以下命令:
sudo mkdir -p /Downloads && sudo chown www-data:www-data /Downloads && sudo chmod 755 /Downloads
- 修复缓存目录权限
报错提示缓存目录不存在,先修复Laravel缓存目录权限:
php artisan storage:link chmod -R 775 storage/framework/cache chown -R www-data:www-data storage
内容的提问来源于stack exchange,提问作者Yorkata008
相关产品推荐
相关产品推荐

