[英]Save an uploaded image via an API call in Laravel 4
I'm not sure what's going wrong here. 我不确定这里出了什么问题。 This is being done by cURL so excuse the formatting of the output.
这是通过cURL完成的,请原谅输出的格式。 I can't save an image I've made via an API call.
我无法保存通过API调用制作的图像。 This code:
这段代码:
//snip
if(Input::hasFile('photo')) {
var_dump(Input::file('photo')); die();
}
produces: 产生:
<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=1)</i>
0 <font color='#888a85'>=></font>
<b>object</b>(<i>Symfony\Component\HttpFoundation\File\UploadedFile</i>)[<i>9</i>]
<i>private</i> 'test' <font color='#888a85'>=></font> <small>boolean</small> <font color='#75507b'>false</font>
<i>private</i> 'originalName' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'moon-rising.jpg'</font> <i>(length=15)</i>
<i>private</i> 'mimeType' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'image/jpeg'</font> <i>(length=10)</i>
<i>private</i> 'size' <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>90400</font>
<i>private</i> 'error' <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>0</font>
</pre>
However this code: 但是这段代码:
//snip
if(Input::hasFile('photo') {
var_dump(Input::file('photo')->getSize()); die();
}
prodices the error Call to a member function getSize() on a non-object
. 将错误标记
Call to a member function getSize() on a non-object
。
I have no idea how to get this to work. 我不知道如何使它工作。 I got the same error when initially using the
move()
method to save the file. 最初使用
move()
方法保存文件时,出现了相同的错误。
This problem is fixed. 此问题已解决。 The error is with how I was querying the API with cURL.
错误与我如何使用cURL查询API有关。 By using the command
cURL -F "photo[]=@/path/to/image.jpg"
I was sending a request of multiple files whilst only sending one. 通过使用命令
cURL -F "photo[]=@/path/to/image.jpg"
我发送了多个文件的请求,而只发送了一个。 Thus the Input::file()
instance was an array and not and object, thus Input::file()->move()
didn't work. 因此
Input::file()
实例是一个数组,而不是对象,因此Input::file()->move()
不起作用。 Making sure only a single photo was uploaded “fixed” the issue. 确保仅上传一张照片“解决”了问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.