代码之家  ›  专栏  ›  技术社区  ›  Sid Heart

图像必须是以下类型的文件:jpeg、bmp、png、jpg。在laravel 5.5中

  •  1
  • Sid Heart  · 技术社区  · 8 年前

    我不明白为什么它不起作用,让我出错

    这是我的表格

    {!! Form::open(['method' => 'PUT', 'route' => ['admin.project.edit', $project->slug, 'files' => 'true']]) !!}
    
                          <div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
                        {!! Form::label('title', 'Title') !!}
                        {!! Form::text('title', $project->title, ['class' => 'form-control', 'required' => 'required']) !!}
                        <small class="text-danger">{{ $errors->first('title') }}</small>
                    </div>
                    <div class="form-group{{ $errors->has('content') ? ' has-error' : '' }}">
                        {!! Form::label('content', 'Content') !!}
                        {!! Form::textarea('content', $project->content, ['class' => 'form-control', 'required' => 'required']) !!}
                        <small class="text-danger">{{ $errors->first('content') }}</small>
                    </div>
    
                    @if($project->progress == 1)
                    <div class="radio{{ $errors->has('progress') ? ' has-error' : '' }}">
                        <label for="progress">
                            {!! Form::radio('progress', '1',  null, ['id' => 'radio_id', 'checked' => 'checked']) !!} In Progress
                        </label>
                        <small class="text-danger">{{ $errors->first('progress') }}</small>
                    </div>
    
                    <div class="radio{{ $errors->has('progress') ? ' has-error' : '' }}">
                        <label for="progress">
                            {!! Form::radio('progress', '2',  null, ['id' => 'radio_id']) !!} Complete
                        </label>
                        <small class="text-danger">{{ $errors->first('progress') }}</small>
                    </div>
                    @else
                    <div class="radio{{ $errors->has('progress') ? ' has-error' : '' }}">
                        <label for="progress">
                            {!! Form::radio('progress', '1',  null, ['id' => 'radio_id']) !!} In Progress
                        </label>
                        <small class="text-danger">{{ $errors->first('progress') }}</small>
                    </div>
    
                    <div class="radio{{ $errors->has('progress') ? ' has-error' : '' }}">
                        <label for="progress">
                            {!! Form::radio('progress', '2',  null, ['id' => 'radio_id', 'checked' => 'checked']) !!} Complete
                        </label>
                        <small class="text-danger">{{ $errors->first('progress') }}</small>
                    </div>
                    @endif
    
                    <div class="form-group{{ $errors->has('image') ? ' has-error' : '' }}">
                        {!! Form::label('image', 'Select Image of page') !!}
                        {!! Form::file('image') !!}
                        <p class="help-block">for better view select 1920x1080 size of image</p>
                        <small class="text-danger">{{ $errors->first('image') }}</small>
                    </div>
                        </div>
                        <div class="modal-footer">
                          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                          {!! Form::submit("Edit", ['class' => 'btn btn-warning pull-right']) !!}
    
                      {!! Form::close() !!}
    

    和我的管理面板

    public function projectedit($id, Request $request){
    
        $this->validate($request, [
            'title' => 'required|max:255',
            'content' => 'required|max:10000',
            'image' => 'mimes:jpeg,bmp,png,jpg',
        ]);
    
        $project = Project::where('slug', $id)->firstorfail();
        $project->title = $request->title;
        $project->slug = str_slug($project->title, '-');
        $project->content = $request->content;
        $project->progress = $request->progress;
        if($request->hasFile('image')) {
            $file = Input::file('image');
            //getting timestamp
            $timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());
            $name = $timestamp. '-' .$file->getClientOriginalName();
            $file->move(public_path().'/images/project/', $name);
            $project->image = $name;
            $thumb = Image::make(public_path().'/images/project/' . $name)->resize(1200,500)->save(public_path().'/images/project/thumb/' . $name, 60);
        }
        $project->save();
        return Redirect::back()->with('status', 'Project Edit Success');
    
    }
    

    请告诉我我做错了什么一切都很好,但图像没有上传,没有上传图像,只有其他一切都很好。

    5 回复  |  直到 8 年前
        1
  •  1
  •   Insax    8 年前

    你应该设置

    'files' => 'false'

    'files' => 'true'

    在表格的第1行

        2
  •  1
  •   Yubaraj Shrestha    8 年前

    更改以下行:

    {!! Form::open(['method' => 'PUT', 'route' => ['admin.project.edit', $project->slug, 'files' => 'false']]) !!}
    

    为此:

    {!! Form::open(['method' => 'PUT', 'route' => ['admin.project.edit', $project->slug], 'files' => 'true']) !!}
    

    你上传图像的方式很混乱,控制器看起来很凌乱,用 Laravel Filesystem 为了这个?

    更改配置/文件系统中的以下行。php:

    'local' => [
         'driver' => 'local',
         'root' => storage_path('app'),
    ],
    

    为此:

    'local' => [
         'driver' => 'local',
         'root' => public_path(),
    ],
    

    然后在控制器中可以执行以下操作:

    public function projectedit($id, Request $request){
        $this->validate($request, [
            'title' => 'required|max:255',
            'content' => 'required|max:10000',
            'image' => 'mimes:jpeg,bmp,png,jpg',
        ]);
    
        $project = Project::where('slug', $id)->firstorfail();
        $project->title = $request->title;
        $project->slug = str_slug($project->title, '-');
        $project->content = $request->content;
        $project->progress = $request->progress;
        if($request->hasFile('image')) {
            $project->image = $request->file('image')->store('images/project');
        }
        $project->save();
        return Redirect::back()->with('status', 'Project Edit Success');
    }
    

    清洁权利?:)

        3
  •  0
  •   kofoworola    8 年前

    在表单创建代码中设置:

    ['files' => 'false']
    

    这会限制文件上载,当您将其更改为true时,生成的表单的编码类型为

    enctype= multipart/form-data laravel
    
        4
  •  0
  •   Viraj Shah    8 年前

    设置 “文件”=“gt;”假' “文件”=“gt;”真的' 以你的形式。

    并检查您的php。的ini文件 file_upload=打开;

        5
  •  0
  •   halfer Jatin Pandey    8 年前

    请更改表单方法,如:

    {!! Form::open(['method' => 'PUT', 'route' => ['admin.project.edit', $project->slug, 'files' => 'false']]) !!}
    

    {!! Form::open(['method' => 'POST', 'route' => ['admin.project.edit', $project->slug, 'files' => 'true']]) !!}