Search code examples
phpzend-frameworkzend-file

how to get the path and file name of a uploaded file in Zend framework


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

    }

Solution

  • 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