【问题标题】:how to get the path and file name of a uploaded file in Zend frameworkZend框架如何获取上传文件的路径和文件名
【发布时间】:2012-12-30 14:02:36
【问题描述】:

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

    }

【问题讨论】:

  • 如果文件上传成功,如果您自己没有提供任何路径,则应该在TEMP文件夹中,否则上传的文件将出现在您指定的文件夹中,文件名将不变
  • @Joddy 我在表单类中提供了路径。但我想从我的控制器那里得到它。错误信息是什么意思:方法getFileName不存在
  • getFileName 不是有效的 FORM OBJECT METHOD,您需要在 Zend File Transfer Adapter Http OBJECT 而不是 FORM OBJECT 上调用它
  • 使用 Zend_File_Transfer_Adapter_Http() 检查它是否适合你,或者正确设置你的 Zend_Form_Element_File。这将起作用。

标签: php zend-framework zend-file


【解决方案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

【讨论】:

  • 你能告诉我如何只获取文件名而不是路径
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 2017-05-31
  • 1970-01-01
  • 2013-04-22
  • 2021-01-10
  • 2010-12-31
相关资源
最近更新 更多