2017-02-28 118 views
1

我正在使用laravel 5.3.30并使用图像干预助手上传图像。上传图像时,出现错误“Image.php 143行中的NotWritableException:无法将图像数据写入路径(/ home/test/Desktop/Laravel/blog/public/images /)”。我已经为目录设置了适当的权限,但仍然无法上传图片。无法在Laravel上载图像

代码上传图片文件:

if($request->hasFile('featured_image')){ 
      $image = $request->file('featured_image'); 
      $filename = time().'.'.$image->getClientOriginalExtension(); 
      $location = public_path('images/', $filename); 
      Image::make($image)->resize(800,400)->save($location); 
      $post->image = $filename; 
     } 

我在网上查了解决方案,但没有它的工作。请纠正我,如果我做错了什么。提前致谢。

+0

错误:NotWritableException在Image.php行143:无法写入图像数据到路径(/ home/test/Desktop/Laravel/blog/public/images /)“上传图片 – vivek321

+0

只是为了确认,图片目录已经存在,对吗? – camelCase

+0

@camelCase - 是的,它已经存在 – vivek321

回答

0

运行以下命令来修复权限:

chmod -R 755 /home/Desktop/Laravel/blog/public 

如果它会不工作,如果它是一个本地机器,尝试777

+0

是的,它是本地机器,也尝试过,但它没有奏效。 命令行:chmod -R 777/home/test/Desktop/Laravel/blog/public – vivek321

3

这里是另一种方式。考虑将其存储在storage目录中,而不是存储在public中。这是Laravel recommended way

它可能存储在storage/app/public/images目录内。然后用php artisan storage:link命令创建符号链接。

这样存储使用此路径时:

$location = storage_path('app/images/', $filename); 

创建符号链接后,您可以访问图像URL像 http://your-site.app/storage/images/qwerqrewqerwq.jpg