DEV Community

Cover image for Fix Laravel storage:link issue when symlink is disabled on a Shared hosting
Kadiri Talitu (Coded Salis)
Kadiri Talitu (Coded Salis)

Posted on • Originally published at Medium

Fix Laravel storage:link issue when symlink is disabled on a Shared hosting

The Laravel framework is arguably one of the best backend framework out there, if not the best. But as we all know, it is not suitable to be hosted on a shared hosting account like most traditional PHP frameworks because of the advanced and premium features it supports. As a developer one has to learn to improvise and hack his way around to make things work as desired.

When working with Laravel, there are times you will be faced with the trouble of having to host it on a shared hosting account due to budget reasons, or maybe its just a side project you're experimenting with.

Most hosting providers do not allow their shared hosting users access to symbolic links a.k.a symlink for security reasons, and that is a big issue when you try hosting your Laravel projects with them. This is because Laravel uses symlink function to link images from the Storage directory to the public directory in order for the files to be accessible by the public.
When you try to use the symlink functionality you will be greeted with an error message telling you that such function does not exist. When you try contacting the hosting providers they'll tell you that it was deliberately disabled for security reasons.


Well, you don't have to panic, what you should do is to change the upload location from the storage to the public directory so that you won't have to link the files again, and here is how you should go about that:

Default config for  raw `config/filesystems.php` endraw

In your Laravel project, open the config/filesystems.php file and make the following changes



'local' => [
  'driver' => 'local',
  'root' => public_path('/'),
  'throw' => false,
 ],
'public' => [
   'driver' => 'local',
   'root' => public_path('/'),
   'url' => env('APP_URL').'/storage',
   'visibility' => 'public',
   'throw' => false,
 ],


Enter fullscreen mode Exit fullscreen mode

As you can see, all you need to do is change storage_path('app') to public_path('/') which means our uploads will be saved to the public directory. If you wish you can change that / to whatever sub-directory you want them saved to. eg: /images or /videos 

You can put whatever you like as the path inside the public_path() method call, just configure it to suite your code, upload your files and check the public directory, and you'll find them there.

That is all, if you have any questions kindly drop it in the comments section

Top comments (1)

Collapse
 
ndotie profile image
ndotie

Hey, good guide thanks.
but in shared hosting we put contents of public folder to root folder.
even then index.php from public folder we put it to roots.

will this public_path() not that root is the new public where i can put all my assets and images and static files?