0

我有允许用户上传文件的表单。从我的控制器中,我想获取带有文件名的路径。我正在使用 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);
            }
        }

    }
4

1 回答 1

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 框架:使用 Zend 表单元素上传文件

于 2012-12-30T14:34:03.750 回答