File Upload using zend framework 1.7.4
Asked Answered
G

3

5

I am trying to upload a file using Zend Framework 1.7.4, but have not been successful. I have read Akrabat's tutorial, which was helpful but when i used those techniques in my project I was not able to get it to work.

Goodyear answered 20/3, 2009 at 8:26 Comment(1)
please add more information/code/errormessages what exactly doesn't work.Paranoiac
C
25

The link you posted is just a general Zend Framework tutorial, and hasn't been updated past ZF 1.5.

Anyway, once you get started with Zend, this is a sample of the code you would use to receive an upload. The form doing the posting must have the correct file upload components.

//validate file
//for example, this checks there is exactly 1 file, it is a jpeg and is less than 512KB
$upload = new Zend_File_Transfer_Adapter_Http();
$upload->addValidator('Count', false, array('min' =>1, 'max' => 1))
       ->addValidator('IsImage', false, 'jpeg')
       ->addValidator('Size', false, array('max' => '512kB'))
       ->setDestination('/tmp');

if (!$upload->isValid()) 
{
    throw new Exception('Bad image data: '.implode(',', $upload->getMessages()));
}

try {
        $upload->receive();
} 
catch (Zend_File_Transfer_Exception $e) 
{
        throw new Exception('Bad image data: '.$e->getMessage());
}

//then process your file, it's path is found by calling $upload->getFilename()
Chrisman answered 26/4, 2009 at 14:20 Comment(0)
P
8

Don't forget to set the enctype attribute of the form to "multipart/form-data". If you are using Zend_Form, call

$form->setAttrib('enctype', 'multipart/form-data');

Also note that Zend_Form::setDestination is deprecated, use the rename filter for that:

// Deprecated:
// $upload->setDestination('/tmp');
// New method:
$upload->addFilter('Rename', '/tmp');
Preciosa answered 8/7, 2009 at 19:6 Comment(0)
E
0
    $this->setAction('/sandbox/example/form')->setEnctype('multipart/form-data')->setMethod('post');

    $photo = new Zend_Form_Element_File('photo');
    $photo->setLabel('Photo:')->setDestination(APPLICATION_PATH ."/../public/tmp/upload");

    $this->addElement($photo);

You can set any destination example $photo->setLabel('Photo:')->setDestination(APPLICATION_PATH ."/../data");

Expertise answered 31/10, 2012 at 10:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.