【问题标题】:How can I upload image in local folder laravel?如何在本地文件夹 laravel 中上传图片?
【发布时间】:2021-12-25 07:34:50
【问题描述】:

我是幼虫开发的新手,我无法在本地文件夹中上传图像,并且我收到错误 getClientOriginalExtension() on null。即使我在表单中添加了代码 enctype="multipart/form-data" ,但仍然出现错误。任何人都可以提出如何解决这个问题的建议。我在下面给出了我的代码。 MyBlade 文件

        ****MyBlade file****

                   <form action="{{ route('register.post') }}" method="POST" enctype="multipart/form-data" accept-charset="utf-8"  >
                      @csrf
                     
                      <div class="form-group row">
                          <label for="uname" class="col-md-4 col-form-label text-md-right">User Name</label>
                          <div class="col-md-6">
                              <input type="text" id="username" class="form-control" name="username" required />
                              @if ($errors->has('username'))
                                  <span class="text-danger">{{ $errors->first('username') }}</span>
                              @endif
                          </div>
                      </div>
                       <div class="form-group row{{ $errors->has('image') ? ' has-error' : '' }}">
                        <label for="name" class="col-md-4 col-form-label text-md-right">Profile Picture</label>
                        <div class="col-md-6">
                        <input id="image" type="file" class="form-control" name="image">
                    </div>
                 </div>

我的控制器文件

       if ($request->input('image')!=null){
        $files = $request->input('image');

        $extension = $files->getClientOriginalExtension();
        $filaname = time().','.$extension;
        $files->move('public/image/',$filaname);
       // $post->image= $filaname;
     }
       else{
         return $request;
       }
        




          

我收到错误消息:调用 bool 上的成员函数 getClientOriginalExtension()

【问题讨论】:

  • 你为什么要把$request-&gt;hasfile('image')分配给$files。即使你想比较它,也没有任何意义。
  • 嗨谢谢您的回复。我现在已经编辑了我的代码,请现在检查它是否处于其他状态。你能给出这个问题的解决方案吗?
  • 阅读the docs
  • 我认为这个 - $files = $request->input('image'); - 应该是 $files = $request->file('image');或 $files = $request->image;
  • 是的,实际上图像已成功上传到本地文件夹,但数据库表存储的图像名称如下“C:\xampp\tmp\php2D22.tmp”

标签: php mysql laravel-5


【解决方案1】:

如果您使用$request-&gt;file('image') 而不是$request-&gt;input('image'),您已经解决了问题。然后图像将被移动到public/image 文件夹。

但是,如果您想将文件上传到storage/public 文件夹,您可以使用$file 上的storeAs() 方法将其存储在storage 文件夹中。

// Your controller
public function update(Request $request, $postId)
{
    if (! $request->hasFile('image')) {
        return $request;
    }

    // Use file() instead of input()
    $file = $request->file('image');

    $extension = $file->getClientOriginalExtension();

    // Note you have a typo in your example, you're using a `,` instead of a `.`
    $filename = time().'.'.$extension;

    // To store (move) the file to the 'public/image' folder, use this:
    $file->move('image', $filename);

    // To store the file to the 'storage/app/public/image' folder, use this:
    $file->storeAs('public/image', $filename);

    // Find your post based on some id
    $post = Post::find($postId);

    // Save the filename to the database on the Post model:
    $post->image = $filename;
    $post->save();
}

希望我能帮到你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 2017-10-05
    • 2017-02-25
    • 1970-01-01
    相关资源
    最近更新 更多