Laravel“文件上传错误调用非对象上的成员函数getClientOriginalName()”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27452716/
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
Laravel "File Upload Error Call to a member function getClientOriginalName() on a non-object"
提问by shamelesshacker
all. I am trying to learn Laravel and I am working on uploading an image. I get the following error:
全部。我正在尝试学习 Laravel,并且正在上传图片。我收到以下错误:
"Call to a member function getClientOriginalName() on a non-object"
“在非对象上调用成员函数 getClientOriginalName()”
I am using these packages:
我正在使用这些软件包:
"anahkiasen/former": "dev-master",
"intervention/image": "dev-master",
"intervention/imagecache": "2.*"
After using SO, I have verified the following are NOT contributing to the error above:
使用 SO 后,我已验证以下内容不会导致上述错误:
- multipart/form-data on form
- Upload file data exists
- The PHP.ini max_filesize is way larger than the size of this small test file
- 表单上的多部分/表单数据
- 上传文件数据存在
- PHP.ini max_filesize 比这个小测试文件的大小大得多
My form is:
我的表格是:
<form enctype="multipart/form-data" accept-charset="utf-8" class="form-horizontal" id="create_form" method="POST" action="/elements">
<div class="control-group"><label for="img[]" class="control-label">Upload Image</label><div class="controls"><input multiple="true" class="myclass" accept="image/gif|image/jpeg|image/png" id="img[]" type="file" name="img[]"></div></div>
<div class="form-actions"><input class="btn-large btn-primary btn" type="submit" value="Submit"> <input class="btn-large btn-inverse btn" type="reset" value="Reset"></div>
<input type="hidden" name="_token" value="B0AJ0Y5LMrMng6CsePeZfNSvRQ0KexowOGTK99Gm">
</form>
The code to generate the error is:
产生错误的代码是:
$image = Input::file('img');
$filename = $image->getClientOriginalName();
print_($filename);
If I print out the object using:
如果我使用以下方法打印对象:
print_r($image);
打印_r($图像);
...then I get:
...然后我得到:
Array
(
[0] => Symfony\Component\HttpFoundation\File\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] =>
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => storageunit.jpg
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
[size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 8734
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
[pathName:SplFileInfo:private] => /tmp/php9AU1OE
[fileName:SplFileInfo:private] => php9AU1OE
)
)
All of which looks correct to me, so I am stumped.
所有这些对我来说都是正确的,所以我很难过。
If anyone has any ideas as to what to try next, I would appreciate the help.
如果有人对下一步尝试什么有任何想法,我将不胜感激。
回答by Madbreaks
Look at what's printed out. $image
is an array of objects, not an object. Try:
看看打印出来的东西。 $image
是一个对象数组,而不是一个对象。尝试:
$filename = $image[0]->getClientOriginalName();
回答by shamelesshacker
Doh!
呸!
"file" not "files"
“文件”不是“文件”
{{
Former::file('img')
->label('Upload Image')
->class('myclass')
->accept('gif', 'jpg', 'png');
}}
回答by mangesh zanke
For me the only issue was the class file which the File Upload entity requires was missing.
对我来说,唯一的问题是缺少文件上传实体所需的类文件。
I added this code and it worked,
我添加了这段代码,它起作用了,
use Illuminate\Http\UploadedFile;
使用 Illuminate\Http\UploadedFile;