Laravel 5图像上传失败

问题描述 投票:0回答:1

我使用laravel 5.7,当我尝试上载图片并返回$请求,我可以看到图像上传但是当我尝试将文件存储在我的公用文件夹它显示的图像上传失败。问题是什么。我搜索在计算器的解决方案。我有这么多。但是,他们没有为我工作。

我的商店方法的代码

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');
}

形式Codes-

<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">

laravel image laravel-5 image-uploading laravel-5.7
1个回答
0
投票

如果你有正确的文件夹的权限,那么所有的代码是寻找好的仅为$ upload_path变量路径添加一个反斜杠像$ upload_path =“上传/后”;

© www.soinside.com 2019 - 2024. All rights reserved.