2016-12-16 88 views
0

由于某些原因,我无法使用干预工具保存图片。我之前做过这件事,我在这里也遵循同样的事情,但现在它没有被保存。错误即时得到的是:无法将图像数据写入路径

NotWritableException in Image.php line 143: 
Can't write image data to path (static/photos/1481902023.jpg) 

这里是控制器我的存储功能:

  $this->validate($request, array(
     'title'=>'required|max:255', 
     'slug'=>'required|min:3|max:255|unique:posts', 
     'body'=>'required', 
     'img'=>'image', 
     'desc'=>'max:255' 
     )); 

    $post=new Post; 
    $post->title=$request->title; 
    $post->slug=$request->slug; 
    $post->body=$request->body; 
    $post->desc = $request->desc; 
    if($request->hasFile('img')){ 
     $image=$request->file('img'); 
     $imageName=time().'.'.$image->getClientOriginalExtension(); 
     $location='static/photos/'.$imageName; 
     Image::make($image)->save($location); 
     $post->image=$imageName; 
    } 

    $post->save(); 
    $post->tags()->sync($request->tags, false); 
    $post->categories()->sync($request->categories, false); 
    /*Session::flash('success', 'The Post Has been published!');*/ 
    return redirect()->route('slug', $post->slug); 

这应该是这样的,可是当我看到它并没有出于某种原因,我想也是,它不工作了。

public_path() 

该文件夹本身(静态)位于项目的根目录中。

的上传表单的看法是这样的:

{{csrf_field()}}

 <div class="{{$errors->has('img')?'has-error':''}}"> 
     <label class="label" for="img">Upload picture</label> 
      <div> 
     <input type="file" name="img" id="img" class="input" multiple="multiple"> 
       </div> 
      </div> 

除了这一切,我也检查了许可到该文件夹​​(和子文件夹),甚至运行命令

icacls "C:\Users\GabMic\b\static" /grant GabMic:F 

我在这里失踪了什么? Ps,即时通讯使用最新版本的laravel。谢谢您的帮助。

+0

确保,你给777个权限上传路径目录! –

+0

这只是你的文件夹 – Beginner

+0

的许可你试过包装你的位置到'public_path'函数吗? – Beginner

回答

0

尝试下面的代码。首先声明你的路径,然后验证文件夹是否存在。

if($request->hasFile('img')){ 
    $image=$request->file('img'); 
    $path = base_path("static/photos/"); 
    File::exists($path) or File::makeDirectory($path, 0777, true, true); 
    $img = Image::make($image->getRealPath())->save($path . time().'.'.$image->getClientOriginalExtension(); 
} 
+0

我试过这个。没有运气。在你的代码中你已经声明了路径为'static/photos /'的 – GabMic

+0

。但该文件夹位于与公用文件夹或基本应用程序文件夹相关的位置? – xhulio

+0

它与公众处于同一水平 – GabMic

-3

尝试:

$location='../public/static/photos/'.$imageName; 

$location='../storage/static/photos/'.$imageName; 
     Image::make($image)->save($location); 
+0

嗨!欢迎来到S.O.我不确定在Laravel中使用相对路径是否是好的做法。无论如何,OP已经尝试使用绝对路径,并且它不起作用。最后,请您为此解决方案的工作原理添加一些细节。 –

+0

欢迎。你是对的,这不是好的做法,但我不能以其他方式保存图像。我没有清楚的答案。我认为对象“图像”试图保存在供应商目录中。 –