在共享主机上显示laravel存储的图像 [英] Displaying laravel stored images on shared hosting

查看:55
本文介绍了在共享主机上显示laravel存储的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在实时服务器上成功部署了我的第一个laravel应用程序.一切看起来都很不错,但事实是我无法显示要上传到 /myproject_src/storage/app/public/myfolder1文件夹.

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.

这是我在HostGator上的文件夹层次结构:

Here is my folder hierarchy on HostGator:

/myproject_src/

/myproject_src/

这里是所有laravel源文件(公共文件夹除外)

Here are all the laravel source files (except the public folder)

/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

此路径与已上传到storage/app/public/myfolder1/此文件夹的图像相关联,并且是通过laravel的store('public/myfolder1');方法生成的.

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.

我该怎么做才能在img标签中正确显示图像:

What should I do in order to display the images properly in a img tag:

<img src="{{ how to point to the uploaded image here }}">

推荐答案

好,您可以使用

php artisan storage:link

并使用

<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') }} />

注意:出于灵活性考虑,我建议不要在db中存储路径.请仅存储文件名,然后在代码中执行以下操作.

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;
});

并使用

http://example.com/storage/image.jpg

希望有帮助:)

这篇关于在共享主机上显示laravel存储的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆