1

I cannot understand how ->isuploaded() works. I am suppose to upload six images to display on my index page. Now the problem is, in my update function, if I upload only one or two image $upload->isUploaded() returns a false value, but if I decide to update all six of them it returns a true value. How do I deal with this problem? Am i missing out something here?

Here is my zend file transfer upload

$upload = new Zend_File_Transfer();
$upload->addValidator('Count', false, array('min' =>1, 'max' => 6)) 
               ->addValidator('Size', false, array('max' => '1Mb'))
               ->addValidator('ImageSize', false, array('minwidth' => 50,
                                                        'maxwidth' => 1000,
                                                        'minheight' => 50,
                                                        'maxheight' => 1000));

if ($upload->isUploaded()) $hasImage = true;
Varun
  • 45
  • 1
  • 4

2 Answers2

2

By default Zend guess all uploaded files are invalid even if just one of submitted form file fields was empty. Zend docs are suggest to override this behavior by calling isValid() method before receive(). So I'm not sure if suggest best solution, but it works for me:

$upload = new Zend_File_Transfer();
$upload->setDestination( 'some your destination' );
if( $adapter->isValid( 'your form file field name' ) ){
    $adapter->receive( 'your form file field name' );
}

And so on with every file field name. Wrap in foreach if needed.

Alfred
  • 21,058
  • 61
  • 167
  • 249
zelibobla
  • 1,498
  • 1
  • 16
  • 23
1

Use isValid() instead.

if ($upload->isValid()) {
    // success!
} else {
    // failure!
}

Once you know your upload passed the validators, then start processing the images.

Niklas
  • 29,752
  • 5
  • 50
  • 71
  • @Niklas Yup tried that too. But it is not working. It is just not ready to set $hasImage as true if I don't upload 6 images. – Varun Jun 07 '11 at 16:24
  • @Varun `$upload->isValid()` is returning `false`? – Niklas Jun 07 '11 at 16:25
  • @Nklas Yes! as it is not setting the $hasImage to true. But yes if I upload all of them it sets it to true. – Varun Jun 07 '11 at 16:30
  • @Varun Can you please post the rest of your code then, because there is something else wrong if it isn't validating it. – Niklas Jun 07 '11 at 16:32
  • @Niklas According to documentation "sUploaded($files = null): This method will check if the specified files have been uploaded by the user. **This is useful when you have defined one or more optional files**. When no files are specified, all files will be checked." What does optional files suppose to mean? How do I define optional files? – Varun Jun 07 '11 at 16:33
  • @Varun post the rest of your code and it would be easier to fix the issue – Niklas Jun 07 '11 at 16:34
  • `if ($hasImage) { echo "Inside Bol Has Image"; $files = $upload->getFileInfo(); print_r($files); //And I try to save it in the database here } ` So it should display $files and the echo statement! but it doesn't. – Varun Jun 07 '11 at 16:45
  • @Varun was more looking for the stuff before that... as $hasImage is always false as you said. – Niklas Jun 07 '11 at 16:52