0

I've a image and some text file in storage folder other than the public folder. the folder architecture is storage |->uploads

and set a route like

Route::get('storage/{folder}/{filename}','FileController@getFile');

inside the Controller the getFile method have following code

  $path = storage_path($folder . DIRECTORY_SEPARATOR . $filename);
if (extension_loaded('fileinfo')) {
        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;
    }
    else{
        abort(412,'please enable the fileinfo extension or contact site administration');
    }

when i try to retrieve the image from url it return the blank image like this blank image

but when i try to retrive the text file it return the file

Also tried it with Image::make($path) and response()->file($path) when the mime type is image/* still the output is same.. so How do i show my image file from url without using the public folder

  • Possible duplicate of [How should I serve an image with Laravel?](https://stackoverflow.com/questions/36066144/how-should-i-serve-an-image-with-laravel) – Shobi Aug 19 '18 at 13:38
  • already tried the solution [from here](https://stackoverflow.com/questions/36066144/how-should-i-serve-an-image-with-laravel) but it won't work – rocco.thapa Aug 19 '18 at 13:58

1 Answers1

0

This is how i do it... I also check for user permissions but i adapted it to your needs...

public function getFile()
{
    $path = storage_path($folder . DIRECTORY_SEPARATOR . $filename);

    return response()->file($path);
}
Erubiel
  • 2,934
  • 14
  • 32
  • used that as well but didn't get the file. The file path is correct and file exist as well . – rocco.thapa Aug 19 '18 at 13:41
  • What happens when you print the path? by the way, i usually store the path within the DB not just the filename... This is not how i calculate the route... i just adapted my code to yours. :/ – Erubiel Aug 19 '18 at 13:43
  • @erubile it return the full file path ` "E:\ecommerce\storage\uploads\7ef33eac189a8006c22af9ce66a2b096.jpg"` and when entering this path in explorer address bar the file opened and also tried in browser url bar with file protocol file:///E:/ecommerce/storage/uploads/7ef33eac189a8006c22af9ce66a2b096.jpg and it worked as well – rocco.thapa Aug 19 '18 at 13:45
  • what kind of permissions do the folder and the enclosed content have ? does everyone have permission to read? – Erubiel Aug 19 '18 at 13:48
  • Also, i don't think we use the same dev enviroment, i use vagrant homestead... my paths does not look like that – Erubiel Aug 19 '18 at 13:49
  • This is how my paths look: //home/vagrant/Code/myproject/storage/uploads/contactos/6917/image/1534686660SomeSome.png – Erubiel Aug 19 '18 at 13:54
  • yes all user have full permission on the folder and that is the default path formate of windows – rocco.thapa Aug 19 '18 at 13:54
  • Also i've other text file like pdf and text as well and the same code return the text files – rocco.thapa Aug 19 '18 at 13:55
  • mmmm what happens if you append the file protocol to either your code or the one i passed you? – Erubiel Aug 19 '18 at 13:56
  • if this worked file:///E:/ecommerce/storage/uploads/7ef33eac189a8006c22af9ce66a2b096.jpg – Erubiel Aug 19 '18 at 13:57
  • this also should work: return response()->file('file:///E:/ecommerce/storage/uploads/7ef33eac189a8006c22af9ce66a2b096.jpg'); – Erubiel Aug 19 '18 at 13:57