Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel storage link won't work on production

Tags:

php

laravel

I'm using storage_path to save my images and I symbolic my storage folder to public_html

php artisan storage:link

On the local everything works fine, when I upload an image it will upload in storage folder and link of it will appear in public folder but since I moved to live host and production mode my images will upload in storage folder but nothing in my public_html/storage and I'm not able to get them in my front-end.

Codes

/config/filesystems.php

'public' => [
  'driver' => 'local',
  'root' => storage_path('app/public/'),
  'url' => env('APP_URL').'/storage',
  'visibility' => 'public',
],

.env

FILESYSTEM_DRIVER=public

controller sample

if ($request->hasFile('image')) {
  $image = $request->file('image');
  $filename = 'page' . '-' . time() . '.' . $image->getClientOriginalExtension();
  $location = storage_path('app/public/images/' . $filename);
  Image::make($image)->resize(1200, 600)->save($location);
  if(!empty($page->image)){
    Storage::delete('images/' . $page->image);
  }
  $page->image = $filename;            
}

any idea?

like image 532
mafortis Avatar asked Jun 06 '18 21:06

mafortis


People also ask

How do I link my storage to server in Laravel?

You need to connect to it via ssh, then run php artisan storage:link from terminal. aurawindsurfing's answer is correct but If for whatever reason you do not have access to ssh (which you should) you could alternatively look at the Artisan facade which allows you to make artisan calls from your code.

How do I link my storage folder in Laravel 8?

You can link the storage folder in laravel using php artisan command and you can also run a PHP script to create a symbolic link between two folders.

How do I unlink storage in Laravel 8?

Steps: cd to your <project_root>/public directory and run rmdir storage - it will remove the link.


3 Answers

I see that the question has been answered but I want to suggest another solution which i found to be easy. Go to the route folder your-app-folder/routes/web.php and register a new route as follows

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

then go to your website www.example.com/linkstorage and it will take you to a blank page. that's it. The storage folder will be create. I hope it helps someone.

like image 161
Chetam Okafor Avatar answered Oct 17 '22 20:10

Chetam Okafor


Delete folder storage from public and run this command in cron job (one time):

ln -s /home/public_html/storage/app/public /home/dev5/public_html/public/storage
like image 18
Abdel Rahman Kamhawy Avatar answered Oct 17 '22 20:10

Abdel Rahman Kamhawy


I just encountered this problem, and this is how I fixed it, without the need of SSH'ing into the server or running a CronJob:

For Laravel 5+ (Haven't tested with lower version)

You may use Artisan::call() method inside your route/controller to execute artisan commands without using the terminal.

$exitCode = Artisan::call('storage:link', [] );
echo $exitCode; // 0 exit code for no errors.
like image 15
Mostafa Mohsen Avatar answered Oct 17 '22 20:10

Mostafa Mohsen