【问题标题】:I can not upload image in laravel 5.2我无法在 laravel 5.2 中上传图片
【发布时间】:2016-06-10 11:55:25
【问题描述】:

我是 laravel 框架的初学者。我已经完成了所有的工作,但是当我尝试上传图像时,它会调用未定义的方法 Illuminate\Support\Facades\Request::file() 错误。我已经导入了所有必需的库。那么我没有发现的错误是什么。

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

这是我的用户输入表单。

        <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() !!}

当我在 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)

【问题讨论】:

    标签: php image upload frameworks laravel-5.2


    【解决方案1】:

    为了解决这个问题,您需要导入底层请求类以便正确注入。在文件顶部添加以下内容:

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

    【讨论】:

    • 我已经添加了给定的库,但它会调用字符串上的成员函数 move()。
    猜你喜欢
    • 2018-04-17
    • 2016-08-16
    • 2016-08-18
    • 2016-09-01
    • 2020-03-21
    • 2020-10-03
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多