Liza
Liza

Reputation: 308

how to implement file upload in Zend?

I have a simple form that has email and file to upload, it's displaying correctly, and after the submit it goes to the result page i set, correctly. But, i can't find the file on the server now where.

here is my code:

Form:

 public function init()
    {
        /* Form Elements & Other Definitions Here ... */

        $this->setName('form-counting');

        $email = new Zend_Form_Element_Text('email');
        $email -> setLabel('Name:')
                    ->addFilter('StripTags')
                    ->addValidator('NotEmpty', true)
                    ->addValidator('EmailAddress')
                    ->setRequired(true);
        $this->addElement($email);

        $UP_file = new Zend_Form_Element_File('UP_file');
        $UP_file->setLabel('Upload files:')
            ->setRequired(true)
        $this->addElement($UP_file);


        $this->addElement('submit', 'submit', array(
            'label'    => 'GO',
            'ignore'   => true
        ));

    }

what am i doing wrong? Thanks

Upvotes: 3

Views: 372

Answers (2)

Mehdi
Mehdi

Reputation: 4318

On controller you need an adapter to receive the file. Zend_File_Transfer_Adapter_Http is an easy solution. You should set the receiving destination there in the adapter which receive the file.

Controller:

public function indexAction()
{
    // action body
    $eform = new Application_Form_Eform();
    if ($this->_request->isPost())
    {
        $formData = $this->_request->getPost();
        if ($eform->isValid($formData))
        {
            $nameInput  =   $eform->getValue('email');

            //receiving the file:
            $adapter = new Zend_File_Transfer_Adapter_Http();
            $files = $adapter->getFileInfo();

            // You should know the base path on server where you need to store the file.
            // You can put the server local address in Zend Registry in bootstrap,
            // then have it here like: Zend_Registry::get('configs')->...->basepath or something like that.
            // I just assume you will fix it later:
            $basePath = "/your_local_address/a_folder_for_unsafe_files/";

            foreach ($files as $file => $info) {
                // set the destination on server:
                $adapter->setDestination($basePath, $file);

                // sometimes it would be a good idea to rename the file.
                // I left it for you to do it here:
                $fileName = $info['name'];
                $adapter->addFilter('Rename', array('target' => $fileName), $file);
                $adapter->receive($file);

                // You can make sure of having the file:
                if (file_exists($filePath . $fileName)) {
                    // you can move, copy or change the file before redirecting to the new page.
                    // Or you might need to keep the track of files and email in a database.
                } else {
                    // throw error if you want.
                }
            }
        } 

        // Then redirect:
        $this->_helper->redirector->gotoRouteAndExit (array(
            'controller' => 'index',
            'action'     =>'result', 
            'email'      => $nameInput)); 

    }


    $this->view->form = $eform;
}

Upvotes: 1

Ronak K
Ronak K

Reputation: 1567

Yo should include ,

$file->setDestination(BASE_PATH . '/public/files') // the path you want to set

in your form,

$file = new Zend_Form_Element_File('file');
        $file->setLabel('Upload CSV file:')
            ->setDestination(BASE_PATH . '/public/files')
            ->setRequired(true)
            ->addValidator('NotEmpty')
            ->addValidator('Count', false, 1);
        $this->addElement($file);

as above..

in Controller you can do as following,

 if ($this->_request->isPost()) {
      $formData = $this->_request->getPost();
          if ($form->isValid($formData)) {    
          $uploadedData = $form->getValues();
          Zend_Debug::dump($uploadedData, '$uploadedData');
          Zend_Debug::dump($fullFilePath, '$fullFilePath'); //[for file path]
       }
     }

and you should have defined the following in your bootstrap.php

if(!defined('BASE_PATH')) {
    define('BASE_PATH', dirname(__FILE__) . '/..');
}

use this example for more understanding, ZEND FILE UPLOAD

Change Your form to this,

class forms_UploadForm extends Zend_Form
{
    public function __construct($options = null)
    {
        parent::__construct($options);
        $this->setName('upload');
        $this->setAttrib('enctype', 'multipart/form-data');

        $description = new Zend_Form_Element_Text('email');
        $description->setLabel('Email')
                  ->setRequired(true)
                  ->addValidator('NotEmpty');

        $file = new Zend_Form_Element_File('file');
        $file->setLabel('File')
            ->setDestination(BASE_PATH . '/files')
            ->setRequired(true);

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Upload');

        $this->addElements(array($description, $file, $submit));

    }
}

Upvotes: 3

Related Questions