How to create laravel storage symbolic link for production or sub domain system?

71,893

Solution 1

I solved this problem by another command for creating a symbolic link by terminal/cmd/shh:

ln -s /path/to/laravel/storage/app/public /path/to/public/storage

I solved this also Using laravel web route routes/web.php

Route::get('/foo', function () {
    Artisan::call('storage:link');
});

Solution 2

Another simple way is to run Executing php artisan storage:link Command Programmatically

On routes/web.php

Route::get('/foo', function () {
Artisan::call('storage:link');
});

Solution 3

assuming that your laravel project "public" directory and "public_html" directory are different. Or your subdomain pointer directory may be different , then use your subdomain pointer directory like "public_html" below given process.

apply command: $ php artisan storage:link

this command will create symlink to "public" directory to your laravel project. not inside the "public_html" . so we need to move the "storage" link to "public_html" directory. for this you can move directly. if it doesn't work then move it using command . go inside the "public" directory and run the below command according to your "public_html" directory location.

$ mv storage /home/yourHostinInfo/domains/domainName/public_html

the path might be different based on your hosting information.

Solution 4

In case anyone is looking for another flexible solution using php:

Route::get('/link', function () {        
   $target = '/home/public_html/storage/app/public';
   $shortcut = '/home/public_html/public/storage';
   symlink($target, $shortcut);
});

And make any required change to the $target variable and the $shortcut upon your files structure.

You can use the above code inside that web route or inside any other function.

Solution 5

Creating symbolic link in a web server using SSH

In Laravel documentation, a symbolic link (symlink or soft link) from public/storage to storage/app/public should be created to make files accessible from the web.

(THIS PROCEDURE WILL CREATE SYMBOLIC LINK WITHIN THE LARAVEL PROJECT DIRECTORY)

Here are the steps on how you can create symbolic link in your Linux web server using SSH client:

1. Connect and login to your web server using SSH client (e.g. PUTTY).

2. Link storage/app/public to public/storage using the syntax.

ln -s target_path link_path

Example (in CPanel File Directory)

ln -s /home/cpanel_username/project_name/storage/app/public /home/cpanel_sername/project_name/public/storage
Share:
71,893

Related videos on Youtube

Mizanur Rahman Khan
Author by

Mizanur Rahman Khan

I have 7 years' experience in the area “Software design and Development”. Now I am working at “Yakoun Inc. Ltd” in the post of “Full Stack Developer” Remote Job, Location (107 Valley Oaks Loop, Union City CA, USA) from July 2019 to Continue. My work responsibilities are Yakoun Ecommerce Software (iOS and Android) and web app development Technology: Laravel (PHP), React JS, MySQl, Redis Web: https://yakoun.com/ iOS: https://apps.apple.com/US/app/id1512738197 Android: https://play.google.com/store/apps/details?id=com.yakoun.yakoun Flex Music Software (iOS and Android) and web app development Technology: Laravel (PHP), React JS, MySQl, Redis, Video Editor using ffMPeg-php, ffmpeg-mobile, android-ffmpeg, ios-ffmpeg Web: https://flexmusix.com/ iOS: https://apps.apple.com/us/app/flex-musix/id1574289158 Android: https://play.google.com/store/apps/details?id=com.flexmusix Flex Videos Software (iOS and Android) and web app development Technology: Laravel (PHP), React JS, MySQl, Redis, Video Editor using ffMPeg-php, ffmpeg-mobile, android-ffmpeg, ios-ffmpeg

Updated on July 12, 2022

Comments

  • Mizanur Rahman Khan
    Mizanur Rahman Khan almost 2 years

    When I worked on laravel local development server php artisan storage:link works fine for me. But when I transfer my site to production server then I saw my public storage link was a folder. Then I delete that tried to create a link. I got an error because my app was in the root folder and tried to solve this problem.

  • Vishnoo Rath
    Vishnoo Rath about 5 years
    in my case I had to runt this command with the full path i.e $HOME/domain.com/storage/app/public $HOME/public_html/storage . Hostgator shared hosting
  • Anoop P S
    Anoop P S almost 5 years
    This command works fine in local but gives error in live symblink error
  • Anoop P S
    Anoop P S almost 5 years
    how can i run this
  • Haritsinh Gohil
    Haritsinh Gohil almost 5 years
    how can we run terminal in cpanel ?
  • Alaksandar Jesus Gene
    Alaksandar Jesus Gene almost 5 years
    Delete public/storage folder and run php artisan storage:link again. It worked for me.
  • Kamlesh
    Kamlesh over 4 years
    @MizanurRahmanKhan it is showing an error in command terminal. 'ln' is not recognized as an internal or external command, operable program or batch file.
  • Kamlesh
    Kamlesh over 4 years
    YOU ARE MY HERO, Saved My Life, Thank You :)
  • Kamlesh
    Kamlesh over 4 years
    It works on server only when i set it in cron job to run once.
  • Kamlesh
    Kamlesh over 4 years
    Route::get('/foo', function () { Artisan::call('storage:link'); }); It is a perfect solution.
  • Mizanur Rahman Khan
    Mizanur Rahman Khan over 4 years
    If you don't have server terminal access then you may use Route::get('/foo', function () { Artisan::call('storage:link'); });
  • Rajan Rawal
    Rajan Rawal about 4 years
    Before executing your command, dont forget to check the user. The owner of the project folder and the user by which you create symlink should match. Also check for the needful folder permission. This two helped to get this working.
  • Ridha Rezzag
    Ridha Rezzag about 4 years
    Life saver thank you bro works great but i had to delete the storage file existe in the public folder, then create anew symlink, thanks
  • Saeed
    Saeed over 3 years
    symlink(): No such file or directory
  • Sagor Islam
    Sagor Islam over 3 years
    just apply this 2nd method in web.php(route) and see the result. work perfectly.
  • Mohsin AR
    Mohsin AR almost 3 years
    symlink() has been disabled for security reasons
  • Mohsin AR
    Mohsin AR almost 3 years
    symlink() has been disabled for security reasons
  • mahdi marjani moghadam
    mahdi marjani moghadam over 2 years
    very nice solution :)