Symlink Laravel Storage Folder on Shared Hosting

By default Laravel store all uploaded files to storage folder so whenever we need to get that files to show our template then we need to symlink laravel storage folder to your public folder. So let talk about Symlink Laravel Storage Folder on Shared Hosting.

At Local Environment you can run php artisan storage:link command that will generate a storage linked folder into public folder of your project but when you upload your project on Shared Hosting there you will not get terminal or command prompt so that make problem to create symlink for your storage folder on live server.

Symlink Laravel Storage Folder on Shared Hosting

There are two method you can symlink your storage folder to your public folder on shared hosting.

Before using any method keep in mind that you don’t have storage directory in public directory , if you have first remove.

Method 1

This is very simple method and frequently used by me and other many developers when they upload Laravel project on Shared Hosting. Just add some line code in routes/web.php file

Basic to Advance Routing in Laravel

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

then save your file and open your domain with symlink url.

example – http://example.com/symlink

It will automatically create a linked storage folder in your public directory.

After that you can delete the above code.

Method 2

Using this method you have two choice where you can select anyone which is more comfortable for you.

Choice One – Create File in Public Folder

you can create file with any name except that exist before. For Example symlink.php is perfect name. Here we will use symlink() function that available in php core so you don’t need to create own functions.

PHPCopy
 <?php
$target =$_SERVER['DOCUMENT_ROOT'].'/storage/app/public';
$link = $_SERVER['DOCUMENT_ROOT'].'/public/storage';
symlink($target, $link);
echo "Done";
?> 

After that just open you domain with symlink.php url in browser.

For Example – http://example.com/symlink.php

After complete Just delete symlink.php file from public folder.

Choice Two – Create new Route in web.php

As i told you above that is a choice you can select one or two its depend on you.

PHPCopy
Route::get('/symlink', function () {
   $target =$_SERVER['DOCUMENT_ROOT'].'/storage/app/public';
   $link = $_SERVER['DOCUMENT_ROOT'].'/public/storage';
   symlink($target, $link);
   echo "Done";
});

After that just open you domain with symlink url in browser.

For Example – http://example.com/symlink

After complete Just remove the route and you ready to go.

Conclusion

Thats all, I hope its save your day. These method are useful and you don’t need to remember all things just bookmark this page and whenever you need to symlink your folder copy and paste code and then type URL and delete code and you are ready to rock.

Leave a Reply

Your email address will not be published. Required fields are marked *