【问题标题】:Laravel 5 Image upload failedLaravel 5 图片上传失败
【发布时间】:2019-06-30 22:21:20
【问题描述】:

我正在使用 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">

【问题讨论】:

  • 请向我们展示您的 html 表单。错误信息是什么?
  • 您的服务器设置是什么?你在使用共享资源吗?你的文件夹有什么权限?我建议也许摆脱验证并一次将它们添加回来,这样你就可以看到你什么时候失败了。您使用的是哪个网络服务器?您的网络服务器文件大小限制是多少?
  • 我使用的是共享主机。我尝试删除验证,但没有奏效。我还检查了文件大小。我正在尝试上传 10-20 KB 的图片。
  • Ant 的错误信息是“图片无法上传”
  • 在路径 $upload_path = 'uploads/post' 中添加“/”;发布后

标签: laravel image laravel-5 image-uploading laravel-5.7


【解决方案1】:

如果您对该文件夹具有正确的权限,那么所有代码​​看起来都正常,只有 $upload_path 变量路径添加反斜杠,如 $upload_path = 'uploads/post';

【讨论】:

    猜你喜欢
    • 2016-08-21
    • 2018-12-15
    • 1970-01-01
    • 2021-04-29
    • 2019-05-15
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 2016-04-11
    相关资源
    最近更新 更多