简体   繁体   中英

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. I can't save an image I've made via an API call. 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'>=&gt;</font>
      <b>object</b>(<i>Symfony\Component\HttpFoundation\File\UploadedFile</i>)[<i>9</i>]
      <i>private</i> 'test' <font color='#888a85'>=&gt;</font> <small>boolean</small> <font color='#75507b'>false</font>
      <i>private</i> 'originalName' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'moon-rising.jpg'</font> <i>(length=15)</i>
      <i>private</i> 'mimeType' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'image/jpeg'</font> <i>(length=10)</i>
      <i>private</i> 'size' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>90400</font>
      <i>private</i> 'error' <font color='#888a85'>=&gt;</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 .

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.

This problem is fixed. The error is with how I was querying the API with cURL. By using the command cURL -F "photo[]=@/path/to/image.jpg" I was sending a request of multiple files whilst only sending one. Thus the Input::file() instance was an array and not and object, thus Input::file()->move() didn't work. Making sure only a single photo was uploaded “fixed” the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM