CodingLaravel

Images Not Showing After Deploying Laravel App – Solved

I have been working on a Laravel project and I deployed it to a web hosting account. The application allows users to upload profile pictures and update (replace) them whenever they want to. After uploading the application to the hosting server, I was dismayed to find that if a user uploaded a new profile picture, the new picture was not displayed on the site. This feature was working fine on my computer. I tried changing paths and using explicit URLs, to no avail.

I asked ChatGPT how to fix this and it suggested that I use SSH to run the command

php artisan storage:link

In order to do this on the hosting account, I downloaded an SSH client and eventually managed to log in to the account and run the command.
I got an error that ‘File not found.’ I don’t know what file (or was it folder?) was missing.

I then turned to Google and found that this problem of images not showing after an application is deployed is fairly common. However, there seem to be many different solutions. The most common was what ChatGPT had already suggested.

Someone on one website suggested executing the same command through Laravel by creating a route and function as follows:

Route::get(‘/create-symlink’, function (){
symlink(storage_path(‘/app/public’), public_path(‘storage’));
echo “Symlink Created. Thanks”;
});

Before that, I deleted the ‘storage’ folder from the public folder (often named’public_html’).

I tried running the function and again got ‘folder not found’ error. I then realised that I needed to specify the path to the folders of interest.

I asked ChatGPT how I should structure the command.

I then added a route in the format below to my route file, web.php. (Note the absolute path).

Route::get(‘/create-symlink’, function () {
symlink(‘/home/path/to/domain/app/folder/storage/app/public’, ‘/home/path/to/public/folder/public_html/storage’);
echo “Symlink Created. Thanks”;
});

I then went to mydomain.com/create-symlink.

That was it! The symlink was created and the images were being displayed as desired!