Laravel storage link won't work on production

2020-03-27 11:29发布

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?

标签: php laravel
7条回答
啃猪蹄的小仙女
2楼-- · 2020-03-27 11:43

You also can delete your folder storage:link first then open terminal in host and command php artisan storage:link . If your hosting support symlink it will works

查看更多
ゆ 、 Hurt°
3楼-- · 2020-03-27 11:52

Solution: Once your application is uploaded to the live server. Just check that if you are having any storage folder in public directory. Check the folder stucture

Just delete the storage folder from the public directory.

[app/public/storage]

After that from putty software access your main Laravel project folder, and run this command on live.

Remember to configure your .env file APP_URL={your live server ip} and then the below command.

php artisan storage:link

It will create a new storage link in public folder associated with the APP_URL .

查看更多
劫难
4楼-- · 2020-03-27 11:54

SOLVED

Thanks for all your helps, I tried to run php artisan storage:link on my server and I found out that my server has disabled symlink for security reason.

So I have changed all my image uploading functions to upload images in my public folder instead of storage folder and now everything is working just fine.

查看更多
你好瞎i
5楼-- · 2020-03-27 11:59

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

查看更多
淡お忘
6楼-- · 2020-03-27 12:06

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.
查看更多
Deceive 欺骗
7楼-- · 2020-03-27 12:06

change storage_path settings by public_path

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

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

if that does not work, you can try:

$path = base_path().'/../public_html/images'; 
查看更多
登录 后发表回答