简体   繁体   English

如何在本地文件夹laravel中上传图像?

[英]How can I upload image in local folder laravel?

I am new in larval development, I am unable to upload image in local folder and I am getting error getClientOriginalExtension() on null.我是幼虫开发的新手,我无法在本地文件夹中上传图像,并且在 null 上收到错误 getClientOriginalExtension()。 Even I have added code enctype="multipart/form-data" in my form, but still am getting the error.即使我在表单中添加了代码 enctype="multipart/form-data",但仍然出现错误。 Can anyone give suggestion how to resolve this issue.任何人都可以提出如何解决这个问题的建议。 I have given below my code.我在下面给出了我的代码。 MyBlade file我的刀片文件

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

My Controller File我的控制器文件

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




          

I am getting err:Call to a member function getClientOriginalExtension() on bool我收到 err:Call to a member function getClientOriginalExtension() on bool

If you use $request->file('image') instead of $request->input('image') you have fixed the problem.如果你使用$request->file('image')而不是$request->input('image')你已经解决了这个问题。 The image will then be moved to the public/image folder.然后图像将被移动到public/image文件夹。

However, if you want to upload the file to the storage/public folder, you can use the storeAs() method on the $file to store it in the storage folder.但是,如果要将文件上传到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();
}

Hope I helped you with this.希望我能帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将本地Laravel Web项目上传到服务器? - How can I upload my local Laravel web project to server? 如何在“ img”文件夹中上传Facebook图像? - How can I upload the Facebook image in my “img” folder? 我如何在 Laravel 6 上上传图片? - How I upload an image on laravel 6? 如何将图像上传到Laravel上的其他项目? - How can I upload image to different project on the laravel? 如何使用CodeIgniter在“ upload”文件夹中上传图像的文件名和文件路径? - How can I upload the filename and filepath of the image I uploaded inside “upload” folder using CodeIgniter? 我无法在laravel 5.2中上传图片 - I can not upload image in laravel 5.2 我从Postman上传到Laravel API的图像未保存在该文件夹中 - The image I upload to my Laravel API from Postman is not saved in the folder 如何在上传时从公共文件夹中删除旧图像 - 使用 Laravel - How to delete older image from public folder on upload - using laravel 如何访问公共文件夹laravel中的图像 - How can access image in public folder laravel 我如何使用 react native expo 图像将图像上传到我的本地服务器 PHP/数据库 MySQL - how can i upload image using react native expo image to my local server PHP / Database MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM