2017-04-23 54 views
0

我已经在活动服务器上成功部署了我的第一个laravel应用程序。除了我无法显示正在上传到 /myproject_src/storage/app/public/myfolder1文件夹的图像这一事实,一切看起来都很棒。在共享主机上显示laravel存储的图像

这里是HostGator的我的文件夹层次:

/myproject_src/

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

/的public_html/MYDOMAIN .com/

这里g OES我所有的公共目录

我存储的文件路径到数据库以下列方式的内容:

public/myfolder1/FxEj1V1neYrc7CVUYjlcYZCUf4YnC84Z3cwaMjVX.png

此路径与已经上传到存储/应用的图像关联/ public/myfolder1 /这个文件夹是从laravel的store('public/myfolder1');方法生成的。

我应该为了在img标签正确显示图像做:

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

''你尝试访问? – imrealashu

+0

@imrealashu输出包括我的根目录在内的整个路径。像这样:'/ home3/eisenheim/myproject_src/storage // public/myfolder1/FxEj1V1neYrc7CVUYjlcYZCUf4YnC 84Z3cwaMjVX.png' – Eisenheim

+0

请检查我的答案我认为这会有所帮助。我通常在我的共享主机上使用它。 – imrealashu

回答

1

好了,你可以创建一个使用

php artisan storage:link 

和访问文件,使用

<img src="{{ asset('public/myfolder1/image.jpg') }}" /> 
符号链接

但有时候,如果您在共享主机上,则无法创建符号链接。您希望保护某些访问控制逻辑背后的某些文件,可以选择具有读取和提供映像的特殊路径。例如。

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

现在你可以像这样访问你的文件。

http://example.com/storage/public/myfolder1/image.jpg 
<img src="{{ asset('storage/public/myfolder1/image.jpg') }} /> 

注:我建议不存储在数据库中的灵活性的路径。请只存储文件名并在代码中执行以下操作。

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 

希望帮助:)