0
votes

I'm trying upload a file using zend form. I've tried in so many ways but i'm getting the form post data with file name but file was not receiving. Here is my code

 **My Form code**


public function testUploadForm(){

    $imageviewScript = new Zend_Form_Decorator_ViewScript();
    $imageviewScript->setViewScript('image_element.phtml');

    $image_name = new Zend_Form_Element_File('image_name');
    $image_name->setAttrib('enctype', 'multipart/form-data');
     $image_name->addValidator('Extension',false,'jpg,png,gif');
        $image_name->addValidator('Count', false, 1)
        //$image_name->setRequired(true)
                            ->addDecorator($imageviewScript)
                           ->setDecorators($this->setFieldElementDecorators());

     $submit = new Zend_Form_Element_Submit('submit');
    $submit->setLabel('Submit')
    ->setAttrib('class', 'addbutton_6')
    ->removeDecorator('label')
    ->setDecorators($this->getRightSubmitDecorators());

    $this->addElements(array($image_name,$submit));



}

In controller i'm using to receive data like

public function testuploadAction(){

    $params= array();
    $this->_form->testUploadForm();

    if($this->getRequest()->isPost()){

        $params= $this->getRequest()->getParams();

        $upload= new Zend_File_Transfer_Adapter_Http();
        $upload->setDestination(APPLICATION_PATH.'/../images/uploads/');
        if($uploads->receive()) {
            //Code to process the file goes here
 $filesize = $upload->getFileSize('image_name');

print_r($file_size);exit;

        } else {
            $errors = $upload->getErrors();
        }
        //$formData = $_POST->getValues(); //be sure to call this after receive()
        print_r($formData);exit;
        //$file = $upload->getFileInfo();
        //  $filename = $upload->getFileName('image_name');          //optional info about uploaded file


    }

    $this->_form->populate($params);
    $this->view->form= $this->_form;


}

when I tried to print the data it was showing warnings like below.

Notice: Undefined index: validators in C:\xampp\htdocs\XXXX\library\Zend\File\Transfer\Adapter\Abstract.php on line 493

Warning: array_search() expects parameter 2 to be array, null given in C:\xampp\htdocs\XXXX\library\Zend\File\Transfer\Adapter\Abstract.php on line 493

Notice: Undefined index: tmp_name in C:\xampp\htdocs\XXXX\library\Zend\File\Transfer\Adapter\Abstract.php on line 589

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\XXXX\library\Zend\Validate\File\Count.php on line 228

No Syntax errors nothing. Form was submitting properly but file was not receiving here. I've changed the file element to Zend file element i'm getting below fatal error after submitting the form.

Fatal error: Uncaught exception 'Zend_File_Transfer_Exception' with message '"image_name" not found by file transfer adapter' in C:\xampp\htdocs\XXXX\library\Zend\File\Transfer\Adapter\Abstract.php:1254 Stack trace: #0 C:\xampp\htdocs\XXXX\library\Zend\File\Transfer\Adapter\Abstract.php(572): Zend_File_Transfer_Adapter_Abstract->_getFiles('image_name') #1 C:\xampp\htdocs\XXXX\library\Zend\Form\Element\File.php(435): Zend_File_Transfer_Adapter_Abstract->isValid('image_name') #2 C:\xampp\htdocs\XXXX\library\Zend\Form.php(1987): Zend_Form_Element_File->isValid('shreyansh-ipad ...', Array) #3 C:\xampp\htdocs\XXXX\application\controllers\CustomerController.php(41): Zend_Form->isValid(Array) #4 C:\xampp\htdocs\XXXX\library\Zend\Controller\Action.php(503): CustomerController->registerAction() #5 C:\xampp\htdocs\XXXX\library\Zend\Controller\Dispatcher\Standard.php(285): Zend_Controller_Action->dispatch('registerAction') #6 C:\xampp\htdocs\XXXXX\library\Zend\Controller\Front.php(934): Zend_Co in C:\xampp\htdocs\XXXXX\library\Zend\File\Transfer\Adapter\Abstract.php on line 1254

Can some one plz suggest why the files are not uploading for me. Thank you.

1
Have you tried a var dump on $_FILES?Gohn67

1 Answers

0
votes

Finally I found the solution. It is happening due to the form encryption only. To achieve some sort of website design I'm echoing each field in the view file i.e, .phtml file. So there i'm using the normal form. Even I'm assigning the encryption type to zend form, it was not coming in the phtml form, so the file was not uploading now i set like

<form enctype="multipart/form-data" method="post" action="" >

Now it is working fine. Thanks