Home > Software design >  Can't download files from public folder from Laravel 5.6
Can't download files from public folder from Laravel 5.6

Time:09-10

I don't know what I am doing wrong here, but I can't for the life of me manage to get the download feature to work in Laravel 5.6

I have files saved to the public folder as you can see below:

enter image description here

On the webpage, these images show up correctly in HTML <img> tags and when I view the details of the image, it shows me that it points to https://mydomain.test/storage/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png.

However, when I try and download this file from the server, I constantly get an exception error about the file not existing (for example: "data":"The file \"public\/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png\" does not exist"). I have tried every combination of path string I can think of, but everything results in the same error.

I have tried using storage_path, I have tried using the original path, I've tried hard coding the path

    [2022-09-08 09:58:55] local.ERROR: Failed to download media file: The file "/home/vagrant/Code/dras/storage/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png" does not exist  
    [2022-09-08 09:59:38] local.ERROR: Failed to download media file: The file "public/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png" does not exist  
    [2022-09-08 10:00:14] local.ERROR: Failed to download media file: The file "https://mydomain.test/storage/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png" does not exist   
    [2022-09-08 10:17:18] local.ERROR: Failed to download media file: The file "app/public/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png" does not exist
    [2022-09-08 10:45:50] local.ERROR: Failed to download media file: The file "storage/public/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png" does not exist 

The only combination that doesn't throw an exception about "file does not exist" is when I use the path

return response()->download( 'storage/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png' );

However, that command throws a totally different error:

Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::header() in file /home/vagrant/Code/dras/app/Http/Middleware/Cors.php on line 27

CodePudding user response:

The path:"public/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png\ implies the conflict between windows and linux systems. There cant be both / and \ in a path. it must be either or. Unfortunately, i have been there and there is no easy way to get past this. you must use str_replace method to manually replace either (/) or () with the other. Check what your OS views as a valid file path first. it should be either: \public\LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png or /public/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png

Edit: For future reference, this is what finally resolved the problem,

BinaryFileResponse in Laravel undefined

  • Related