Laravel:文件上传不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48444034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:17:49  来源:igfitidea点击:

Laravel: File upload not working

phplaravellaravel-5

提问by Raj Mehta

I'm trying to upload an image via an HTML form in Laravel 5.5. I have included the enctype="multipart/form-data"attribute, still nothing happens.

我正在尝试通过 Laravel 5.5 中的 HTML 表单上传图像。我已经包含了该enctype="multipart/form-data"属性,但仍然没有任何反应。

Form code:

表格代码:

<form method="POST" enctype="multipart/form-data">
  {{ csrf_field() }}
  <div class="form-group">
    <label for="m_photo" class="col-md-4 control-label">Main photo</label>
    <div class="col-md-6">
      <input id="m_photo" type="file" class="form-control-file space" name="m_photo" required>
    </div>
  </div>

  <div class="form-group">
    <div class="col-md-6 col-md-offset-4">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>
  </div>
</form>

Route (web.php) code:

路由(web.php)代码:

Route::post('smartphones/entry', 'HomeController@s_submit')->name('s_submit');

Controller code:

控制器代码:

public function s_submit() {
  if (Input::hasFile('m_photo')) {
      // doing something
  }

  else {
      echo 'Nothing happened';
  }
}

'Nothing happened' is echoed out when I submit the form.

当我提交表单时,“什么也没发生”被回显出来。

It's interesting that when I do this:

有趣的是,当我这样做时:

public function s_submit(Request $request) {
    $input = Input::all();
    dd($input);
}

I see:

我懂了:

array:1 [
"m_photo" => UploadedFile {#210 ?}
]

It's like the image isgetting passed, but I'm unable to retrieve it. Please help.

就像图像传递了一样,但我无法检索它。请帮忙。

采纳答案by Laerte

This can happen when PHP max_file_size is not set to a size that allows the file you are trying to upload to be sent. This causes hasFilereturns false, when, for example, file->getClientOriginalName()works.

当 PHP max_file_size 未设置为允许您尝试上传的文件被发送的大小时,就会发生这种情况。这会导致hasFile返回 false,例如,当file->getClientOriginalName()有效时。

Try to check upload_max_filesizeor post_max_sizein your php.ini, or try with a smaller file to check if it works.

尝试检查upload_max_filesizepost_max_size在您的 php.ini 中,或尝试使用较小的文件来检查它是否有效。

回答by Alexey Mezenin

Get the file with:

获取文件:

$file = $request->m_photo;

Or with:

或与:

$file = $request->file('m_photo');

https://laravel.com/docs/5.5/requests#retrieving-uploaded-files

https://laravel.com/docs/5.5/requests#retrieving-uploaded-files

回答by Sohel0415

if (Input::hasFile('m_photo')) {
    $destinationPath = '/uploads/app/';
    $file = $request->file('m_photo');
    $filename = $file->getClientOriginalName();
    $file->move(public_path() . $destinationPath, $filename);
    $filename_to_save_in_db = $destinationPath . $filename;
}

回答by Ilya Yaremchuk

Try this:

尝试这个:

public function s_submit()
{
            request()->validate([
                'm_photo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            ]);

            $imageName = time().'.'.request()->m_photo->getClientOriginalExtension();
            request()->m_photo->move(public_path('images'), $imageName);
            return back()
                ->with('success','You have successfully upload image.')
                ->with('m_photo',$imageName);
}

Blade:

刀刃:

{!! Form::open(array('route' => 's_submit','files'=>true)) !!}
<div class="form-group">
    <label for="m_photo" class="col-md-4 control-label">Main photo</label>
    <div class="col-md-6">
{!! Form::file('m_photo', array('class' => 'form-control-file space')) !!}
    </div>
  </div>
  <div class="form-group">
    <div class="col-md-6 col-md-offset-4">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>
  </div>
{!! Form::close() !!}

回答by Saurabh Mistry

You have forgot to put action in Your html Form :

您忘记在您的 html 表单中添加操作:

put action="/smartphones/entry" or action="{{route('s_submit')}}"

put action="/smartphones/entry" 或 action="{{route('s_submit')}}"

<form method="POST" enctype="multipart/form-data" action="{{route('s_submit')}}">
  {{ csrf_field() }}
  <div class="form-group">
    <label for="m_photo" class="col-md-4 control-label">Main photo</label>
    <div class="col-md-6">
      <input id="m_photo" type="file" class="form-control-file space" name="m_photo" required>
    </div>
  </div>

  <div class="form-group">
    <div class="col-md-6 col-md-offset-4">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>
  </div>
</form>

Create images folder in public folder

在公共文件夹中创建图像文件夹

in your controller

在你的控制器中

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;


public function s_submit(Request $request) {
  if($request->hasFile('m_photo')){
            $filenameWithExt=$request->file('m_photo')->getClientOriginalName();
            $filename=pathinfo($filenameWithExt,PATHINFO_FILENAME);
            $extension=$request->file('m_photo')->getClientOriginalExtension();
            $fileNameToStore=$filename.'_'.time().'.'.$extension;
            request()->m_photo->move(public_path('images'), $fileNameToStore);
        }
        else{
           $fileNameToStore='noimage.jpg';      
        } 
}

回答by Dawood Najam

For future visitors, the accepted answer is the right answer. I wanted to share one more thing. I also faced similar issue and I had set the upload_max_filesizeand post_max_sizevariables to 20M, which was quite enough. But I still faced the issue. So, I increased to 500M, then it worked. It was really strange because I was uploading a file of less than 1 MB size.

对于未来的访问者,接受的答案就是正确的答案。我想再分享一件事。我也遇到了类似的问题,我将upload_max_filesizepost_max_size变量设置为 20M,这已经足够了。但我仍然面临这个问题。所以,我增加到500M,然后它起作用了。这真的很奇怪,因为我上传了一个小于 1 MB 的文件。