【问题标题】:The image must be a file of type: jpeg, bmp, png, jpg. in laravel 5.5图像必须是以下类型的文件:jpeg、bmp、png、jpg。在 laravel 5.5 中
【发布时间】:2018-05-07 01:04:48
【问题描述】:

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

这是我的表格

{!! 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');

}

请告诉我我做错了什么,一切正常,但图片没有上传,也没有上传图片,否则一切正常。

【问题讨论】:

标签: php laravel laravel-5 laravel-5.5


【解决方案1】:

你应该设置

'files' =&gt; 'false'

'files' =&gt; 'true'

在表单的第 1 行

【讨论】:

  • 你也是对的,看看它在数组中的路由,路由不应该有文件,所以我只是改变'route' => ['admin.project.edit', $project->slug, 'files ' => 'true'] to 'route' => ['admin.project.edit', $project->slug], 'files' => 'true' 在你的代码上解决这个问题,我会检查你
【解决方案2】:

更改以下行:

{!! 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 呢?

在 config/filesytems.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');
}

清洁对吗? :)

【讨论】:

  • 我找到了答案 Look on route its in array and route should not have files 所以我只是更改 'route' => ['admin.project.edit', $project->slug, 'files ' => 'true'] to 'route' => ['admin.project.edit', $project->slug], 'files' => 'true' 在你的代码上解决这个问题,我会检查你
  • 看起来我在里面输入了,整理好了:) 谢谢
【解决方案3】:

在您设置的表单创建代码中:

['files' => 'false']

这限制了文件上传,当你将其更改为true时,生成的表单将具有编码类型

enctype= multipart/form-data laravel

【讨论】:

    【解决方案4】:

    在您的表单中将 'files' => 'false' 设置为 'files' => 'true'

    并检查您的 php.ini 文件中的 file_upload=on;

    【讨论】:

      【解决方案5】:

      请更改您的表单方法,例如:

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

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

      【讨论】:

        猜你喜欢
        • 2021-03-04
        • 1970-01-01
        • 2018-07-13
        • 1970-01-01
        • 2010-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-10
        相关资源
        最近更新 更多