1

I want to upload an image in Zend-framework.

In Application_Form_Test.php I write following code....

uploadImage = new Zend_Form_Element_File('uploadImage');
$uploadImage->setLabel("Upload Image ")
            ->setRequired(true)               
            ->addValidator('Extension', false, 'jpeg,png')
            ->getValidator('Extension')->setMessage('This file type is not supportted.');

In the testAction() I write following code.....

$upload = new Zend_File_Transfer_Adapter_Http();
$upload->addValidator('Size', false, 52428800, 'image');
$upload->setDestination('uploads');
$files = $upload->getFileInfo();
foreach ($files as $file => $info) {
    if ($upload->isValid($file)) {
         $upload->receive($file);
    }
}

Code is running successfully But I am not getting that image to the destination folder?

What may be the problem....? Please help me.....

Thanks in advance....

2 Answers 2

3

I don't think that the getFileInfo() method is supposed to actually execute the file upload. I believe that in your controller action, you have to either call the getValues() method on the form object, or call the receiveFile() method on the form element.

See http://framework.zend.com/manual/en/zend.form.standardElements.html#zend.form.standardElements.file for the documentation examples.

Sign up to request clarification or add additional context in comments.

Comments

0

An additional note: if you look in Zend_Form_Element_File->receive(), you will see that isValid() is called, so there's no need to clutter your controller with it. Here's what I do:

if ($upload->receive()) {
    if ($upload->getFileName() && !file_exists($upload->getFileName())) {
        throw new Exception('The upload should have worked, but somehow did not!');
}
} else {
    throw new Exception(implode(PHP_EOL, $upload->getErrors()) . implode(PHP_EOL, $upload->getErrorMessages()));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.