3

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

2 Answers 2

3

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));

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

10 Comments

where in the bootstrap.php ?
at the top if you prefer
yes it is for zend 1.12, please turn your error reporting on, and make sure you have a files directory in public folder, or whatever path you are giving exists.
please follow the link i have given, everything is mentioned there in detail , and it has the source code also which you can download.
what is the result of Zend_Debug::dump($uploadedData, '$uploadedData'); ? what path it displays ? and did you followed the article ?
|
1

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;
}

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.