开发者

Upload an image using Zend Framework?

开发者 https://www.devze.com 2023-02-07 18:42 出处:网络
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\');

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....


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.


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()));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消