简体   繁体   English

显示从存储 laravel 上传的图像

[英]show an uploaded image from storage laravel

i want to see an image that i have uploaded on my rest api.我想看看我上传到我的休息 api 上的图像。

here is the file i have uploaded这是我上传的文件

i have try this code,我试过这个代码,

public function MostrarImagenUsuario($id){
    $usuario = Usuario::find($id);
    $link = Storage::disk('public')->url('app/'.$usuario->foto);
    return response()->file($link);
}

but when i tried to test it with postman it sends me this error.但是当我尝试用邮递员测试它时,它向我发送了这个错误。

FileNotFoundException: The file " http://localhost/storage/app/public/IqFviqFca7PPEXQlOK0nnBf11Yg2mXne8xzuR0wh.jpeg " does not exist in file FileNotFoundException:文件“ http://localhost/storage/app/public/IqFviqFca7PPEXQlOK0nnBf11Yg2mXne8xzuR0wh.jpeg ”在文件中不存在

also i have try this method我也试过这个方法

public function MostrarImagenUsuario2($id){
    $usuario = Usuario::find($id);
    $link = Storage::get($usuario->foto);;
    return $link;
}

and i get this result:我得到了这个结果:

second result第二个结果

finally i dont know how to solve this.最后我不知道如何解决这个问题。

your code does not work because you try get image from non public directory in laravel all public asset always store in root public folder.您的代码不起作用,因为您尝试从 laravel 中的非公共目录获取图像,所有公共资产始终存储在根公共文件夹中。 in the case of images you can store in storage/app/public but whwn you want to get image always use public/ folder and it both connected with the sync link.在图像的情况下,您可以存储在 storage/app/public 中,但是您想要获取图像始终使用 public/ 文件夹,并且它都与同步链接连接。

php artisan storage:link

use this command for sync link使用此命令进行同步链接

after run this command 1shortcut created in your root public folder.运行此命令后 1shortcut 在您的根公用文件夹中创建。 and you can all images from root public shortcut.并且您可以从根公共快捷方式中获取所有图像。

$url = Storage::url('database filed name where you store image data');

Make sure you have created storage link by calling php artisan storage:link通过调用 php artisan storage:link 确保您已创建存储链接

Then you can get the url of your storage file.然后你可以得到你的存储文件的 url。

$url = Storage::url('file.jpg'); $url = Storage::url('file.jpg');

Use like this in the model在模型中像这样使用

This function is execute when you get the image property当您获取图像属性时执行此函数

public function getImageAttribute($value)
{
    if (!$value) {
        return 'http://placehold.it/160x160';
    }

    return env("APP_URL")."/storage/phones/".$value;
}

This function is execute when you set the image property from file input当您从文件输入设置图像属性时执行此函数

   public function setImageAttribute($photo)
    {
        $this->attributes['Image'] = move_file($photo, 'phones');
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM