Laravel8 storage link won't work on production

124 Views Asked by At

I have problems with the storage link in Laravel8, everything was working correctly until I moved the project from localhost to hosting, it works with public_html and I have it structured as follows.

LaravelAPP
--app
--boostrap
--config
--database
--storage
--...

public_html
--img
--storage
-- .htaccess
-- index.php
-- ... 

In localhost the public folder was inside the LaravelAPP folder but in the hosting I had to put its content in public_html.

This is the fragment of the function in which I save the documents, and it does it correctly, it saves them in the storage folder that is inside LaravelAPP but it does not do the same in public_html (I fear because it is not doing correctly the symbolic link).

$document->attach_image = $image->storeAs("public/image/$year/$user", $name_image);
$document->attach_pdf = $pdf->storeAs("public/pdf/$year/$user", $name_pdf);

When I save the image or pdf in the application I get the following URL, so it creates well the above code, but it shows me 404 | NOT FOUND because it is not created in public_html and can not show anything.

https://mydomain/storage/pdf/2022/darkvayder/pdf-test.pdf

I tried to add the following in the filesystems.php document, but it was not effective, I think the link to the public folder is still created.

'links' => [
        public_path('public_html/storage') => storage_path('app/public'),
],
1

There are 1 best solutions below

2
Bervetuna On

In case you're on shared hosting and have no access to the command line, you could establish the link to the public folder by using this route in your web.php:

Route::get('/linkstorage', function () { $targetFolder = base_path().'/storage/app/public'; $linkFolder = $_SERVER['DOCUMENT_ROOT'].'/storage'; symlink($targetFolder, $linkFolder); });