获取/读取 laravel 5.8 存储非 public 文件夹文件以查看?
Get / Read laravel 5.8 Storage non public Folder files to View?
Try to access 'storage/app/folder1/a.png' from my view
public function viewStorageFiles()
{
$fileFullPath = Storage::disk('local')->path('folder1/a.png');
$fileUrl = Storage::disk('local')->url('app/folder1/a.png');
$storage_path = storage_path('app/folder1/a.png');
return view('email.fileDownload')->with([
'fileFullPath' => $fileFullPath,
'fileUrl' => $fileUrl,
'storage_path' => $storage_path,
]);
}
In view : email.fileDownload
<div>
<p> asset($fileUrl) ==> {{asset($fileUrl)}}</p>
<img src="{{asset($fileUrl)}}"/>
</div>
<div>
<p> url($fileUrl) ==> {{url($fileUrl)}}</p>
<img src="{{url($fileUrl)}}"/>
</div>
<div>
<p> storage_path($fileUrl) ==> {{storage_path($fileUrl)}}</p>
<img src="{{storage_path($fileUrl)}}"/>
</div>
结果是:
转到 config/filesystems 添加此数组
'public_site' => [
'driver' => 'local',
'root' => public_path('storage'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
]
那么你的函数可能是这样的
public function viewStorageFiles()
{
$fileFullPath = Storage::disk('public_site')->path('folder1/a.png');
$fileUrl = Storage::disk('public_site')->url('app/folder1/a.png');
$public_path = public_path('storage/app/folder1/a.png');
return view('email.fileDownload')->with([
'fileFullPath' => $fileFullPath,
'fileUrl' => $fileUrl,
'storage_path' => $public_path,
]);
}
这个问题可能有很多答案!
您可以 create a symbolic link from "public/storage" to "storage/app/public"
使用以下命令:
php artisan storage:link
以上命令会将您的 storage/app/public
目录映射到 public
目录。
现在假设您的 storage/app/public
目录中有 user1.jpg
和 user2.jpg
文件,您可以通过以下方式访问它们:
http://your-domain.com/storage/user1.jpg
http://your-domain.com/storage/user2.jpg
* 根据您的评论更新了我的回答:*
您可以 return 来自受某些中间件保护的路由的文件响应!
例如 - 遵循路径 returns 来自 storage/app/uploads
无法公开访问的目录的文件响应:
Route::get('storage/{file}', function ($file) {
$path = storage_path('app' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $file);
return response()->file($path);
});
无论如何你都可以保护上面的路由并在你的视图中使用它..
希望对您有所帮助..
Try to access 'storage/app/folder1/a.png' from my view
public function viewStorageFiles()
{
$fileFullPath = Storage::disk('local')->path('folder1/a.png');
$fileUrl = Storage::disk('local')->url('app/folder1/a.png');
$storage_path = storage_path('app/folder1/a.png');
return view('email.fileDownload')->with([
'fileFullPath' => $fileFullPath,
'fileUrl' => $fileUrl,
'storage_path' => $storage_path,
]);
}
In view : email.fileDownload
<div>
<p> asset($fileUrl) ==> {{asset($fileUrl)}}</p>
<img src="{{asset($fileUrl)}}"/>
</div>
<div>
<p> url($fileUrl) ==> {{url($fileUrl)}}</p>
<img src="{{url($fileUrl)}}"/>
</div>
<div>
<p> storage_path($fileUrl) ==> {{storage_path($fileUrl)}}</p>
<img src="{{storage_path($fileUrl)}}"/>
</div>
结果是:
转到 config/filesystems 添加此数组
'public_site' => [
'driver' => 'local',
'root' => public_path('storage'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
]
那么你的函数可能是这样的
public function viewStorageFiles()
{
$fileFullPath = Storage::disk('public_site')->path('folder1/a.png');
$fileUrl = Storage::disk('public_site')->url('app/folder1/a.png');
$public_path = public_path('storage/app/folder1/a.png');
return view('email.fileDownload')->with([
'fileFullPath' => $fileFullPath,
'fileUrl' => $fileUrl,
'storage_path' => $public_path,
]);
}
这个问题可能有很多答案!
您可以 create a symbolic link from "public/storage" to "storage/app/public"
使用以下命令:
php artisan storage:link
以上命令会将您的 storage/app/public
目录映射到 public
目录。
现在假设您的 storage/app/public
目录中有 user1.jpg
和 user2.jpg
文件,您可以通过以下方式访问它们:
http://your-domain.com/storage/user1.jpg
http://your-domain.com/storage/user2.jpg
* 根据您的评论更新了我的回答:*
您可以 return 来自受某些中间件保护的路由的文件响应!
例如 - 遵循路径 returns 来自 storage/app/uploads
无法公开访问的目录的文件响应:
Route::get('storage/{file}', function ($file) {
$path = storage_path('app' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $file);
return response()->file($path);
});
无论如何你都可以保护上面的路由并在你的视图中使用它..
希望对您有所帮助..