Search code examples
phpzend-frameworkzend-http-client

ZendFramework - How to upload multiple files using the zend_http_client?


How to upload multiple files with ->setFileUpload()?

For example i have this:

<form method="post" 
      action="/file/csvupload" 
      target="myadmin_b20301" 
      enctype="multipart/form-data">
  <input type="text" name="test" id="test" value="">
  <input type="file" name="file[]" id="attachment" class="attachment">   
  <input type="file" name="file[]" id="attachment" class="attachment">   
  <input type="file" name="file[]" id="attachment" class="attachment">   
  <input type="submit" name="submit" id="submit" value="Update">              
</form>

PHP: (after main upload, it push to admin panel which is another post).

....
if (!$upload->isValid()) // /file/csvupload
{
....
  $client = new Zend_Http_Client(); // upload now to administrator another copy
  $client->setUri($uri);
  $client->setParameterPost(array('test'=>'test');
  //how do i tell here to use those same file[] which are multiple?   
  $client->setFileUpload('/tmp/Backup.tar.gz', 'bufile');       
  $client->request( );

Solution

  • You can just call setFileUpload() once for each file that you want to upload. Each call adds the file to an array of files that will be uploaded.

    If you would like to upload an array of files to the same file element, change your code to this:

    $client->setFileUpload('/tmp/Backup.tar.gz',    'bufile[]');
    $client->setFileUpload('/tmp/DB.Backup.tar.gz', 'bufile[]');
    

    Use the array bracket notation on the filename just like you would in HTML.

    This results in:

    $_FILES = array(
        'name' => array(
            0 => 'Backup.tar.gz',
            1 => 'DB.Backup.tar.gz',
        ),
        'tmp_name' => array(
            0 => '/tmp/php63832' // Backup.tar.gz
            1 => '/tmp/php33248' // DB.Backup.tar.gz
        ),
        // etc
    );