我已经成功地将我的第一个laravel应用程序部署到一个实时服务器上。除了无法显示上载到/myproject\u src/storage/app/public/myfolder1
文件夹中的图像外,其他一切看起来都很棒。
这是我在HostGator上的文件夹层次结构:
/myproject_src/
以下是所有laravel源文件(公用文件夹除外)
/公共html/mydomain。com/
下面是我在公共目录中的所有内容
我以以下方式将文件路径存储到数据库中:
public/myfolder1/FxEj1V1neYrc7CVUYjlcYZCUf4YnC84Z3cwaMjVX。巴布亚新几内亚
此路径与已上传到存储/app/Public/myfolder1/this文件夹的图像相关联,并且从laravel的store('Public/myfolder1');
方法生成。
我应该怎么做才能在img标签中正确显示图像:
<img src="{{ how to point to the uploaded image here }}">
您可以使用
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
希望有帮助:)
这里的简单答案是手动运行php-artisan-storage:link
命令
首先,删除公共文件夹中的存储文件夹,然后将此代码添加到web.php文件的顶部。
Artisan::call('storage:link');
希望这对你有帮助。
一个简单的工作方法可能是在共享托管ssh终端中运行php artisan存储:link
。然后只需在filesystem.php
中更改公共驱动程序的url
'disks' => [
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/public/storage',
'visibility' => 'public',
],
]