简体   繁体   English

我无法在laravel 5.2中上传图片

[英]I can not upload image in laravel 5.2

I am a beginner with laravel framework. 我是laravel框架的初学者。 I have done all crud but when i try to upload image it gives Call to undefined method Illuminate\\Support\\Facades\\Request::file() error. 我已经完成了所有工作,但是当我尝试上载图像时,它给出了对未定义方法Illuminate \\ Support \\ Facades \\ Request :: file()的调用。 I have imported all required libraries. 我已经导入了所有必需的库。 So what is the error I did not find. 那么我没有发现什么错误。

    This is method for image upload and move 

        public function store(Request $request) {

            $post = Request::all();

            if ($file = $request->file('file_path')) {
                $name = $file->getClientOriginalName();
                $name->move('uploads', $name);
                $post['file_path'] = $name;
            }
            Post::create($post);
            return redirect('post');
        }

This is my form for user input. 这是我的用户输入表格。

        <section>
            <div class="container">
                <div class="row col-md-6 col-md-offset-3">
                    <h3>Add your new post</h3>
                    {!! Form::open(['url' => 'post/store', 'files' => true]) !!}
                    <div class="form-group">
                        {!! Form::label('title', 'Title') !!}
                        {!! Form::text('title', null, ['class' => 'form-control']) !!}
                    </div>
                    <div class="form-group">
                        <select class="form-control" name="category">
                            @foreach($categories as $category)
                            <option value="{{ $category->id }}">{{ $category->category_name }}</option>
                            @endforeach
                        </select>
                    <div class="form-group">
                        {!! Form::label('file_path', 'Image') !!}
                        {!! Form::file('file_path', null) !!}
                    </div>
                    <div class="form-group">
                        {!! Form::label('published_at', 'Publish on') !!}
                        {!! Form::input('date','published_at', date('Y,m,d'), ['class' => 'form-control']) !!}
                    </div>
                    <div class="form-group">
                        {!! Form::label('Description', 'Description') !!}
                        {!! Form::textarea('description', null, ['class' => 'form-control']) !!}
                    </div>
                    <div class="form-group">
                        {!! Form::label('published_by', 'Published By') !!}
                        {!! Form::text('published_by', null, ['class' => 'form-control']) !!}
                    </div>
                    <div class="form-group">                
                        {!! Form::submit('Submit',['class' => 'btn btn-success']) !!}
                    </div>
                    {!! Form::close() !!}

When i print $post variable in var_dump it gives error with : 当我在var_dump中打印$ post变量时,它给出了以下错误:

    D:\wamp64\www\laravel\app\Http\Controllers\PostController.php:19:
    array (size=7)
      '_token' => string '5j1YVXV41NwjeKbTDlYJMrCXp39mECGTyVjhy3Jh' (length=40)
      'title' => string 'Lorem Post' (length=10)
      'category' => string '1' (length=1)
      'published_at' => string '2016,06,11' (length=10)
      'description' => string 'Lorem Ipsum Dolor Imet.' (length=23)
      'published_by' => string 'LaraGeek' (length=8)
      'file_path' => 
        object(Illuminate\Http\UploadedFile)[166]
          private 'test' (Symfony\Component\HttpFoundation\File\UploadedFile) => boolean false
          private 'originalName' (Symfony\Component\HttpFoundation\File\UploadedFile) => string 'annapurna.jpg' (length=13)
          private 'mimeType' (Symfony\Component\HttpFoundation\File\UploadedFile) => string 'image/jpeg' (length=10)
          private 'size' (Symfony\Component\HttpFoundation\File\UploadedFile) => int 20076
          private 'error' (Symfony\Component\HttpFoundation\File\UploadedFile) => int 0
          private 'pathName' (SplFileInfo) => string 'D:\wamp64\tmp\php15A3.tmp' (length=25)
          private 'fileName' (SplFileInfo) => string 'php15A3.tmp' (length=11)

In order to fix that you need to import the underlying request class so that it is injected correctly. 为了解决此问题,您需要导入基础请求类,以便正确注入它。 Add the following at the top of your file: 在文件顶部添加以下内容:

 use Illuminate\Http\Request;
 use Illuminate\Http\Response;
 use Illuminate\Http\UploadedFile;
 use File;
 use Illuminate\Support\Facades\Input;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM