即使上传了文件,Laravel Input::hasFile('image') 也会返回 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25929197/
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 10:07:47  来源:igfitidea点击:

Laravel Input::hasFile('image') returns false even if a File is uploaded

phplaravelfile-uploadlaravel-4.2

提问by jrenk

I have a Form field for an Image upload, which I open with 'files' => true, like so:

我有一个用于图像上传的表单字段,我使用“文件”=> true 打开它,如下所示:

{{ Form::label('image', 'Image') }}
{{ Form::file('image') }}

And in my Controller I want to check if a File was uploaded and do something with it:

在我的控制器中,我想检查文件是否已上传并对其进行处理:

if (Input::hasFile('image')){
        $in_path = 'img/';
        $in_extension = Input::file('image')->getClientOriginalExtension();
        $filename = Input::get('name').".".$in_extension;
        Input::file('image')->move($in_path, $filename);
        $user->image = $filename;
    }

But Input::hasFilealways returns false and I don't know why.

Input::hasFile总是返回false,我不知道为什么。

Input::file('image');

results in:

结果是:

Symfony\Component\HttpFoundation\File\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => test.JPG
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => application/octet-stream
[size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 1
[pathName:SplFileInfo:private] => 
[fileName:SplFileInfo:private] => 
)

I have tested around with another picture for another User and this works fine. I don't get why this is working for some Users and for some others not.
Is there maybe some kind of Pictures that are not accepted?

我已经为另一个用户测试了另一张图片,效果很好。我不明白为什么这对某些用户有效,而对其他用户无效。
是否有某种不接受的图片?

What other sources could be the cause of this problem?

还有哪些其他来源可能是导致此问题的原因?

采纳答案by jrenk

I solved what was wrong. The code is fine, but the problem was some pictures were simply to big.

EDIT:
As Don't Panic pointed out, editing upload_max_filesizecan solve the problem.

我解决了什么问题。代码很好,但问题是有些图片太大了。

编辑:
正如 Don't Panic 所指出的,编辑upload_max_filesize可以解决问题。

回答by Tim

How do you open your form? If you want your form to accept files you need so open it like this:

你怎么打开你的表格?如果您希望表单接受您需要的文件,请像这样打开它:

echo Form::open(array('url' => 'foo/bar', 'files' => true))

Opening a form in the Laravel Docs

在 Laravel 文档中打开表单

回答by Felipe Castillo

I had the same problem, I checked my code and noticed that I had not the enctype="multipart/form-data" header in the form, hope that this big neglect help anyone

我遇到了同样的问题,我检查了我的代码,发现表单中没有 enctype="multipart/form-data" 标头,希望这个大疏忽对任何人都有帮助