在共享主机上显示 Laravel 存储的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43575027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying laravel stored images on shared hosting
提问by Tanmay
I have successfully deployed my first laravel application on a live server. Everything looks great except the fact that I am unable to display the images that are being uploaded to the
/myproject_src/storage/app/public/myfolder1
folder.
我已经在实时服务器上成功部署了我的第一个 Laravel 应用程序。除了我无法显示正在上传到/myproject_src/storage/app/public/myfolder1
文件夹的图像之外,一切看起来都很棒
。
Here is my folder hierarchy on HostGator:
这是我在 HostGator 上的文件夹层次结构:
/myproject_src/
/myproject_src/
Here are all the laravel source files (except the public folder)
这里是所有的 Laravel 源文件(public 文件夹除外)
/public_html/mydomain.com/
/public_html/mydomain.com/
Here goes all my contents of the public directory
这是我在公共目录中的所有内容
I am storing the file path into the database in the following manner:
我以以下方式将文件路径存储到数据库中:
public/myfolder1/FxEj1V1neYrc7CVUYjlcYZCUf4YnC84Z3cwaMjVX.png
public/myfolder1/FxEj1V1neYrc7CVUYjlcYZCUf4YnC84Z3cwaMjVX.png
This path is associated with the image that has been uploaded to storage/app/public/myfolder1/ this folder and is generated from store('public/myfolder1');
method of laravel.
这个路径和已经上传到storage/app/public/myfolder1/这个文件夹的图片相关联,是由store('public/myfolder1');
laravel的方法生成 的。
What should I do in order to display the images properly in a img tag:
我应该怎么做才能在 img 标签中正确显示图像:
<img src="{{ how to point to the uploaded image here }}">
回答by imrealashu
Well, you can create symbolic link using
好吧,您可以使用创建符号链接
php artisan storage:link
and access files using
并使用访问文件
<img src="{{ asset('public/myfolder1/image.jpg') }}" />
But sometime you can't create symbolic link if you're on shared hosting. You want to protect some files behind some access control logic, there is the alternative of having a special route that reads and serves the image. For example.
但有时如果您使用共享主机,则无法创建符号链接。您想保护某些访问控制逻辑背后的某些文件,可以选择使用特殊的路由来读取和提供图像。例如。
Route::get('storage/{filename}', function ($filename)
{
$path = storage_path($filename);
if (!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
});
Now you can access your files like this.
现在您可以像这样访问您的文件。
http://example.com/storage/public/myfolder1/image.jpg
<img src="{{ asset('storage/public/myfolder1/image.jpg') }} />
Note:I'd suggest to not store paths in the db for flexibility. Please just store file name and do the following thing in the code.
注意:为了灵活性,我建议不要在数据库中存储路径。请只存储文件名并在代码中执行以下操作。
Route::get('storage/{filename}', function ($filename)
{
// Add folder path here instead of storing in the database.
$path = storage_path('public/myfolder1' . $filename);
if (!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
});
and access it using
并使用访问它
http://example.com/storage/image.jpg
Hope that helps :)
希望有帮助:)
回答by Bilawal Awan
The simple answer here is run php artisan storage:link
command manually
这里的简单答案是php artisan storage:link
手动运行命令
first, delete the storage folder inside your public folder then add this code to the top of the web.php file.
首先,删除公共文件夹中的存储文件夹,然后将此代码添加到 web.php 文件的顶部。
Artisan::call('storage:link');
Hope this will help you.
希望这会帮助你。