Laravel 无法将图像数据写入路径
由于某些原因,我无法使用干预工具保存图片。我以前也这样做过,我在这里也遵循同样的方法,但现在它没有被保存。我得到的错误是:Laravel 无法将图像数据写入路径,laravel,laravel-5.3,intervention,Laravel,Laravel 5.3,Intervention,由于某些原因,我无法使用干预工具保存图片。我以前也这样做过,我在这里也遵循同样的方法,但现在它没有被保存。我得到的错误是: NotWritableException in Image.php line 143: Can't write image data to path (static/photos/1481902023.jpg) 以下是我在控制器中的存储功能: $this->validate($request, array( 'title'=>
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()
文件夹itselfstatic位于项目的根目录中
上载的表单视图如下所示:
{{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>
我错过了什么?另外,我正在使用最新版本的laravel。谢谢您的帮助。请尝试以下代码。首先声明路径,然后验证文件夹是否存在
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();
}
尝试:
或
请确保,您已授予777个上载路径目录的权限!这只是您的文件夹的权限。您是否尝试将您的位置包装为公共路径函数?@SaumyaRastogi这是本地开发,而不是服务器中的。NewbeeDev,权限是正确的。在您的代码中,您的路径声明与static/photos/一样。但是相对于您的公用文件夹或基本应用程序文件夹,该文件夹位于何处?它与public `$path=base_pathstatic/photos/`处于同一级别用这行代码试试上面的代码文件facade是来自laravel还是干涉?我应该导入什么吗?对不起,文件Facade来自Illumb。添加使用照明\支持\立面\文件;你好欢迎来到S.O.我不确定在Laravel中使用相对路径是否是好的做法。无论如何,OP已经尝试使用绝对路径,但没有成功。最后,请您补充一些关于为什么这个解决方案会起作用的细节。欢迎。你们是对的,这不是一个好的做法,但我不能保存图像的其他方式。我没有明确的答案。我认为对象映像试图保存在供应商目录中。
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();
}
$location='../public/static/photos/'.$imageName;
$location='../storage/static/photos/'.$imageName;
Image::make($image)->save($location);