如何在Zend框架中获取上传文件的路径和文件名

时间:2012-12-30 14:02:36

标签: php zend-framework zend-file

我的表单允许用户上传文件。从我的控制器我想得到文件名的路径。我正在使用getFileName()方法。但它给出了以下错误:

Message: Method getFileName does not exist 

以下是我的控制器操作:

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

    }

1 个答案:

答案 0 :(得分:1)

检查是否有效 -

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

}

我正在讨论的第二种方法。表格文件元素设置     Zend Framework: Upload file by using Zend Form Element