我尝试从本地存储下载文件,但不断收到错误:无法检索位置处的文件的file_size。
if (file_exists($filePath)) {
return Storage::download($filePath, $fileName);
}
先测试文件路径,以确保文件存在。
418错误提示:您访问的页面不存在。
public function fileSize(string $path): FileAttributes
{
$location = $this->prefixer->prefixPath($path);
error_clear_last();
if (is_file($location) && ($fileSize = @filesize($location)) !== false) {
return new FileAttributes($path, $fileSize);
}
throw UnableToRetrieveMetadata::fileSize($path, error_get_last()['message'] ?? '');
}
我在这条线上设置了一个断点
$location = $this->prefixer->prefixPath($path);
发现$location被设置为文件夹路径和完整文件路径的某种奇怪的连接。
C:\Users\tomva\Projects\laravel\storage\app\C:/Users/tomva/Projects/laravel/storage/app/pph/482678575-0150974001684180496/1283_CO1SAL_202301_O.pdf
我想这可能与在Windows上开发有关,所以我上传到我们的Web服务器,这是Linux,但得到了同样的错误。
我测试只是得到文件大小,它抛出相同的错误。
$size = Storage::size($filePath);
但是使用is_file($filePath)
和filesize($filePath)
可以很好地工作;
只是为了确保文件可以下载,我尝试使用一些代码,从另一个应用程序,我已经用来下载文件,它工作正常。
有人知道是什么原因导致的吗?
1条答案
按热度按时间wwodge7n1#
问题是您使用file path作为**storage::download()**的参数。
storage::download()已经有了该目录的路径,你只需要传入一个你想要下载的文件名作为第一个参数。第二个参数是用户下载文件时看到的文件名。
下面是一个例子:
其中**$fileName为原始文件名**,$userShownFileName为用户下载时的文件名。你可以阅读更多关于laravel文档的内容。