Laravel 5.3本地存储路径

时间:2017-02-09 23:15:49

标签: php laravel laravel-5

我有Laravel 5.3和本地文件系统。我的.env中的APP_URL是:

E_ABORT Operation Aborted

我将存储链接到公共

http://localhost/myapp

我有一张图片:

php artisan storage:link

在数据库中,路径保存如下:

myapp/storage/app/public/defaults/test/file.png

所以在我的blade.php中,我尝试访问图像

public/defaults/test/file.png

但是图片没有显示,图片的网址是:

<img src="{{ Storage::url($user->test)}}" width="" alt="">

那为什么忽略了app_url?即使我改变了这个链接:

http://localhost/storage/defaults/test/file.png

图片没有显示出来。

如何使用Storage :: url访问myapp / storage / app / public / defaults / test / file.png中的图像?

BR

2 个答案:

答案 0 :(得分:3)

如果要访问存储文件夹中的文件,则需要为该链接创建路径

Route::get('storage/defaults/test/{filename}', function ($filename)
{
    $imagePath = storage_path() . '/defaults/test/' . $filename;

    if(!File::exists($imagePath)) {
        //Not have file do something here
    };

    $file = File::get($imagePath);
    $type = File::mimeType($path);

    $response = Response::make($file, 200);
    $response->header("Content-Type", $type);

    return $response;
});

现在您可以通过链接访问

您还有另一种方法,例如将图像保存在[您的项目] / public / images /中,这样您就可以http://localhost/images/file.png访问您的图像(无需创建路径)

但我建议您在服务器示例/数据/图像中创建一个新文件夹 并在apache中设置vhost以允许路径

Alias /images /var/www/data/images

<Directory /var/www/data/images>
   IndexOptions FancyIndexing FoldersFirst
   Options MultiViews Indexes
   AllowOverride None
   Order allow,deny
   Allow from all
</Directory>

希望这个帮助

答案 1 :(得分:2)

试试这个:

1)Fire php artisan storage:link命令,如果你还没有

2)在数据库中,存储相对于 root / public 目录的路径,前缀为存储

因此,如果您在root / public / defaults / test / file.png上有图像,那么在数据库中,存储以下路径:storage / defaults / test / file.png

3)最后在视图中,显示如下:

<img src="{{ asset($user->test )}}" width="" alt="">

确保$ user-&gt; test具有值storage / defaults / test / file.png

3)如果您不想将存储前缀存储到数据库中,请存储路径,如:

默认/测试/ file.png

并在视野中:

<img src="{{ asset('storage/' . $user->test )}}" width="" alt="">

注意: 使用Laravel 5.4测试,但很确定它也适用于Laravel 5.3。

这是我创建的完整演示,演示了图片上传和显示:

https://github.com/xparthx/laravel5.4-image-upload