Issues

ZF-4774: Form's Files doesn't work at all

Description

I've form like this:


$subForm = new Zend_Form;
$subForm
    ->addElements(array(
        $subForm->createElement('file', 'mp3')
            ->setLabel('Plik mp3')
            ->setDestination($options['tmp'])
            ->addFilter('Rename', $options['tmp'] . '/' . md5(microtime(true)))
            ->setRequired(true)
            ->addValidator('Count', false, 1) // ensure only 1 file
            ->setMaxFileSize(10 * 1024 * 1024), // limit to 10M
            //->addValidator('FilesSize', true, '10MB')
            //->addValidator('Extension', true, 'mp3') // only JPEG, PNG, and GIFs
        $subForm->createElement('file', 'kar')
            ->setLabel('Plik kar')
            ->setDestination($options['tmp'])
            ->addFilter('Rename', $options['tmp'] . '/' . md5(microtime(true)))
            ->setRequired(true)
            ->addValidator('Count', false, 1) // ensure only 1 file
            //->addValidator('Extension', true, 'kar,mid,midi')
));

(additionaly: commented validators doesn't work!)

When I send files, I've got ONE, first file and null for second (and it hasn't uploaded). setMutiFile() doesn't work to. To get work above code, should apply my patch. It's get proparly value of "content" in getValue() method from Zend_Form_Element_File class, and save value after receive(). Next time, it uses saved value from protected var.

Comments

Patch to repair Zend_Form_Element_File.

Please test against current trunk. Thomas has been making huge strides in fixing bugs and adding feature to the file upload element since the 1.7.0 PR release, and you may find that the issues have already been corrected.

Assigning to Thomas.

Some improvements.

Do the proposed code change the behaviour of the validators ? This would wonder me, because the file validators do not use the elements value.

I don't know. Validators doesn't work without my changes neither (as I wrote in description) - my form always fail if I use validators commented in code above.

So when your code does not change the behaviour, and it works wether with old nor with changed classes, what is the reason of the proposed change ?

Please try r12266 which is the latest release. Some things have been changed. The latest release should also work with your code.

It not repair validators, if validators doesn't use getValue - I think. But example code above doesn't work with version #12265 from trunk. It returns filename and move to selected directory, only first file. In second (and probably next), I haven't any value (null) nor file moved. My proposed change fix this.

As I wrote:

Please try r12266 which is the latest release. Your change does not work with latest release, but this release should fix your problem. ;-)

Now I have two nulls in $form->getValues() (mp3 & kar) and none uploaded files.

In Zend/Form/Element/File.php, after line 588# $content = $this->getTransferAdapter()->getFileInfo($this->getName()); add: 589# $content = reset($content); It should fix.

Sorry, but we are no try-n-error community. ;-)

First you propose to delete the array handling, now you propose to readd it. Do you really know what you want ?

I will readd the old behaviour which, related to your patch, did not work, but must be implemented reading your last comment.

Ok, please use r12267. I readded multifile handling for getValue().

Uh, sorry. I thought my fix worked ok... And I think it now too :P Maybe it's because my english is poor... Anyway

Look

You have:


wich receive all table, with all sent files. And you got first element from it. If I uploaded many files, it broke "getValue" (and upload too).

A suggested:

Now you get table with one element, contains only uploaded file which actualy you want. But! It's still table! So next line:


is always false!
Now, you must get first (and single) element from table:

And, it's as I provided in fix:


-         $content = current($this->getTransferAdapter()->getFileInfo());
+        $content = $this->getTransferAdapter()->getFileInfo($this->getName());
+        $content = reset($content);

... Now you understand?

No you're completly wrong.

The code reads:


        $content = current($this->getTransferAdapter()->getFileInfo($this->getName()));

And this does the same as your 2 last lines... it returns all elements with the actual element's name (can be multiple when using multifile.

And then uses the current one. As we only test if there is any content it does not matter if we have the first or the second one. This does only test if the $_FILES array has been properly initialised or not.

As I said:

PLEASE TRY OUT r12267. The code you gave is from r12265.

Now, after you add "$this->getName()" in param of getFileInfo it works. Thank you.

PS I know, that was r12265, I tried to explain you, where I see error. Nevermind. EOT. You could close it, I think. Thank you.

Fixed with r12267

Changing issues in preparation for the 1.7.0 release.