0

I have form which allow users to upload a file. From my controller I want to get the path with filename. I am using getFileName() method. But it gives the below error:

Message: Method getFileName does not exist 

Below is my controller action:

public function addAction()
    {
        $form = new Application_Form_Student();
        $form->setAttrib('enctype', 'multipart/form-data');
        $form->submit->setLabel('Add');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $name = $form->getValue('name');
                $email = $form->getValue('email');
                $photo = $form->getValue('photo');
                $location = $form->getFileName('photo'); 
                $students = new Application_Model_DbTable_Students();
                $students->addStudent($name, $email);

                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        }

    }
ehp
  • 2,495
  • 6
  • 29
  • 43
  • If the file is uploaded successfully, it should be in the TEMP folder if you yourself have not provided any path otherwise the uploaded file will be present in the folder specified by you, and the name of file would not change – Joddy Dec 30 '12 at 14:13
  • @Joddy I have provided the path in form class. But I want to get it from my controller. what is the meaning of the error Message: Method getFileName does not exist – ehp Dec 30 '12 at 14:16
  • 1
    getFileName is not a valid FORM OBJECT METHOD, you need to call that on a Zend File Transfer Adapter Http OBJECT not FORM OBJECT – Joddy Dec 30 '12 at 14:20
  • use Zend_File_Transfer_Adapter_Http() check out if that works for you, or setup your Zend_Form_Element_File properly. that will work. – Joddy Dec 30 '12 at 14:28

1 Answers1

1

Check if this works -

public function addAction()
{
    $form = new Application_Form_Student();
    $form->setAttrib('enctype', 'multipart/form-data');
    $form->submit->setLabel('Add');
    $this->view->form = $form;

    if ($this->getRequest()->isPost()) {
        $formData = $this->getRequest()->getPost();
        if ($form->isValid($formData)) {
            $upload = new Zend_File_Transfer_Adapter_Http();
            $upload->setDestination("/uploads/files/");


            try { 
                 $upload->receive();
                 $location = $upload->getFileName('photo');
            }                
            catch(Zend_File_Transfer_Exception $e){
                 $e->getMessage();
            }
        } else {
            $form->populate($formData);
        }
    }

}

The Second Method I was talking about. FORM FILE ELEMENT Settings Zend Framework: Upload file by using Zend Form Element

Community
  • 1
  • 1
Joddy
  • 2,557
  • 1
  • 19
  • 26