PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, November 16, 2022

[FIXED] How to return image in Laravel 6?

 November 16, 2022     laravel, laravel-6, laravel-filesystem, laravel-storage     No comments   

Issue

I have this code:

public function showImage($id) {
    $item = File::find($id);

    return response()->file($item->file_name_and_path);
}

$item->file_name_and_path contains the following:

files/2020/04/29/myfile.jpeg

Now I always get a FileNotFoundException.

The image file is on local driver in this path:

{laravel root}/storage/app/files/2020/04/29/myfile.jpeg

How can I get the correct local path or simply return the image file with image HTTP headers?


Solution

You could use the Storage facade for this:

use Illuminate\Support\Facades\Storage;

public function showImage($id) {
    $item = File::find($id);

    return Storage::response($item->file_name_and_path);
}

As you can see here, it will add the following headers to the response:

'Content-Type'
'Content-Length'
'Content-Disposition'


Answered By - Remul
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing