Zend_File_Transfer w/multiple files does not upload equally

江枫思渺然 提交于 2019-12-04 12:48:08

Alright, so I did more digging and it turns out there may be an issue with the way I was chaining addFilter() so I decided to move in a different direction, trying to isolate each file and handle it's upload individually. So far it appears to be working. Here is the revised code:

$data['image'] = (isset($_FILES["image"]) && $_FILES["image"]["name"] ? $_FILES["image"]["name"] : NULL);
$data['file'] = (isset($_FILES["file"]) && $_FILES["file"]["name"] ? $_FILES["file"]["name"] : NULL);

$upload = new Zend_File_Transfer();

$files = $upload->getFileInfo();

$options = array('ignoreNoFile' => TRUE);
$upload->setOptions($options);

foreach ($files as $field => $contents)
{
 if(!strlen($contents["name"]))
 {
  continue;
 }

 // upload instructions for image
 if ($field == 'image')
 {
  $upload->addFilter('Rename', array('target' => RESOURCES_IMG . $data['image'], 'overwrite' => TRUE), 'image')
         ->addValidator('ImageSize', false, array('minwidth'  => 100,
                                                  'maxwidth'  => 100,
                                                  'minheight' => 100,
                                                  'maxheight' => 100), 'image')
         ->addValidator('Extension', false, 'jpg', 'image');
 }

 // upload instructions for file
 if ($field == 'file')
 {
  $upload->addFilter('Rename', array('target' => RESOURCES_FILES . $data['file'], 'overwrite' => TRUE), 'file');
 }

 if(!$upload->receive($field)) {
  echo '<h1>Oops</h1><p>Please correct the following errors: <hr /></p>';

  foreach ($upload->getMessages() as $key => $val)
  {
   echo '<p><strong>' . $key . '</strong><br />' . $val . '</p>';
  }
  die;
  //return;
 }
} // foreach

Pseudo Explanation

I use the getFileInfo() to grab an array of the files available to me then I loop over each. At the beginning of my first for loop I check to see if this file has a name, if it doesn't have a name I assume that field was left blank and is NULL so I tell the loop to skip over that and continue.

Once I'm in the loop I'm just matching my upload directives with the appropriate form field using a simple conditional. The rest should be fairly self-explanatory if you're into Zend stuff.

Hopefully this helps someone else that was in my predicament. If you are a Zend guru maybe you can comment on my solution or fix the bug that's causing the issue. There may be a more "Zend" way of doing it, but it's working now and that feels damn good.

References

Why I thought it was the chaining of the addFilter() method, see the note under Example #3 (below):

Note: Note that even though setting the same filter multiple times is allowed, doing so can lead to issues when using different options for the same filter.

http://framework.zend.com/manual/1.11/en/zend.file.transfer.filters.html

A random blog article that inspired me to try isolating each, I'm calling it, "upload directive" although I'm not sure if that's what it's called:

http://www.pc-freak.net/blog/tag/uploading-multiple-files-from-a-form-with-zend-framework-zf-storing-uploaded-zf-files-with-unique-name/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!