如何通过 URL 地址从存储中打开文件?
How to open file from storage by URL address?
我在存储中存储文件。
不,我尝试生成 link 打开文件,如:
http://[temp.com]/storage/tests/de139607636857fade861a3c2c472643.txt
但是不行。
如何通过 URL 地址从存储中打开文件?
storage
目录存在于网络根目录之外,因为其中的一些内容不一定是公开访问的。
https://laravel.com/docs/5.5/filesystem#the-public-disk
The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in storage/app/public
. To make them accessible from the web, you should create a symbolic link from public/storage
to storage/app/public
. This convention will keep your publicly accessible files in one directory that can be easily shared across deployments when using zero down-time deployment systems like Envoyer.
我强烈建议不要修改 Laravel 的目录结构。
我会做的是创建一个URL来下载文件,例如:
Route::get('download/{file}', 'DownloadsController@index');
然后在该方法的控制器中,放入一些逻辑来断言该文件存在并通过响应下载来提供它,例如
public function index($file)
{
$filePath = storage_path('tests/'.$file);
if (! file_exists($filePath)) {
// Some response with error message...
}
return response()->download($filePath);
}
然后你可以像这样link下载你的文件
http://[temp.com]/download/de139607636857fade861a3c2c472643.txt
例如,控制器的方式允许您在提供文件之前进行一些身份验证检查,或者计算文件的下载次数。
默认情况下url无法访问存储中的文件
您可以使用 Public Disk。为此,您需要创建从 public/storage
到 storage/app/public
的符号 link
从 Larvel 5.3 及更高版本开始,您可以使用 artisan 命令来帮助您创建 symlink
php artisan storage:link
如果您使用的是旧版本 Laravel,您可以找到有关如何创建符号 link
的答案
另一种方法是通过编辑文件 config/filesystems.php
创建新磁盘 "uploads"
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path(),
],
'uploads' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
],
]
在此位置存储文件
Storage::disk('uploads')->put($file_name, $file_content);
并获取文件
asset('uploads/'. $file_name)
我在存储中存储文件。
不,我尝试生成 link 打开文件,如:
http://[temp.com]/storage/tests/de139607636857fade861a3c2c472643.txt
但是不行。
如何通过 URL 地址从存储中打开文件?
storage
目录存在于网络根目录之外,因为其中的一些内容不一定是公开访问的。
https://laravel.com/docs/5.5/filesystem#the-public-disk
The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in
storage/app/public
. To make them accessible from the web, you should create a symbolic link frompublic/storage
tostorage/app/public
. This convention will keep your publicly accessible files in one directory that can be easily shared across deployments when using zero down-time deployment systems like Envoyer.
我强烈建议不要修改 Laravel 的目录结构。
我会做的是创建一个URL来下载文件,例如:
Route::get('download/{file}', 'DownloadsController@index');
然后在该方法的控制器中,放入一些逻辑来断言该文件存在并通过响应下载来提供它,例如
public function index($file)
{
$filePath = storage_path('tests/'.$file);
if (! file_exists($filePath)) {
// Some response with error message...
}
return response()->download($filePath);
}
然后你可以像这样link下载你的文件
http://[temp.com]/download/de139607636857fade861a3c2c472643.txt
例如,控制器的方式允许您在提供文件之前进行一些身份验证检查,或者计算文件的下载次数。
默认情况下url无法访问存储中的文件
您可以使用 Public Disk。为此,您需要创建从 public/storage
到 storage/app/public
从 Larvel 5.3 及更高版本开始,您可以使用 artisan 命令来帮助您创建 symlink
php artisan storage:link
如果您使用的是旧版本 Laravel,您可以找到有关如何创建符号 link
另一种方法是通过编辑文件 config/filesystems.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path(),
],
'uploads' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
],
]
在此位置存储文件
Storage::disk('uploads')->put($file_name, $file_content);
并获取文件
asset('uploads/'. $file_name)