How To Create Symlink For Larave In Shared Hosting

How to create a symbolic link for laravel website in CPanel, that is Laravel Storage Symlink.

There is a simple way to do so, by creating a file and running it. Just follow these steps.

How to create symlink for laravel website in cpanel

Storage symlink

  1. Make sure you do not have a storage folder created in the public folder. (public_html/public/storage). If so then remove it.
  2. Create a PHP file name symlink.php in the root folder, which is in the public_html folder.
  3. And now paste this code.
<?php
$targetFolder = $_SERVER['DOCUMENT_ROOT'].'/storage/app/public'; $linkFolder = $_SERVER['DOCUMENT_ROOT'].'/public/storage'; symlink($targetFolder,$linkFolder); echo 'Symlink process successfully completed';
?>

Create a route

Another step is to quickly create a route and function to create a symlink. Paste the below function in Larave web.php file and hit the link YourSite.com/linkstorage

It will create a new directory called storage in your root directory.

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

Related Posts