我正在使用laravel 5.7,当我尝试上传图像并返回$ request时,我可以看到该图像已上传,但是当我尝试将文件存储在公共文件夹中时,它表明该图像无法上传。问题是什么。我在stackoverflow中搜索了解决方案。我有很多。但是他们都没有为我工作。
我的商店方法代码
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required|min:10|max:200|unique:posts',
'image' => 'image|mimes:jpg,png,gif,jpeg,svg|max:3072',
'body' => 'required',
]);
$post = new Post;
$post->title = $request->title;
$post->user_id = Auth::id();
$post->slug = str_slug($request->title);
$post->body = $request->body;
$post->category_id = $request->category_id;
if ($request->status == '1'){
$post->status = $request->status;
}
$image = $request->file('image');
if ($image){
$image_name = str_random(20);
$ext = $image->getClientOriginalExtension();
$image_fullname = $image_name . '.' . $ext;
$upload_path = 'uploads/post';
$image_url = $upload_path . $image_fullname;
$upload = $image->move(public_path($upload_path), $image_fullname);
if ($upload){
$post->image = $image_url;
}
}
$post->save();
return redirect()->route('admin.post.index')->with('success', 'Post created');
}
表格代码-
<form action="{{route('admin.post.store')}}" method="post" enctype="multipart/form-data">
@csrf
<textarea name="body"></textarea>
<input type="text" name="title" class="form-control" id="titleinput" value="{{old('title')}}">
<select class="form-control" name="category_id" id="category">
<option value="{{$cat->id}}">{{$cat->name}}</option>
</select>
<input type="file" name="image" id="image">
<input type="checkbox" name="status" value="1"> Publish ?
<input type="submit" name="submit">
答案 0 :(得分:0)
如果您对该文件夹具有正确的权限,则所有代码都正常,只有$ upload_path变量路径添加反斜杠,例如$ upload_path ='uploads / post';