我有一個簡單的表單,其中包含要上傳的電子郵件和文件,它顯示正確,提交后,它正確地轉(zhuǎn)到了我設(shè)置的結(jié)果頁面.
但是,我現(xiàn)在找不到服務(wù)器上的文件.
這是我的代碼:
形成:
public function init() { /* Form Elements & Other Definitions Here ... */ $this->setName('form-counting'); $email = new Zend_Form_Element_Text('email'); $email -> setLabel('Name:') ->addFilter('StripTags') ->addValidator('NotEmpty', true) ->addValidator('EmailAddress') ->setRequired(true); $this->addElement($email); $UP_file = new Zend_Form_Element_File('UP_file'); $UP_file->setLabel('Upload files:') ->setRequired(true) $this->addElement($UP_file); $this->addElement('submit', 'submit', array( 'label' => 'GO', 'ignore' => true )); }
我究竟做錯了什么?謝謝
解決方法:
在控制器上,您需要一個適配器來接收文件. Zend_File_Transfer_Adapter_Http是一個簡單的解決方案.您應(yīng)該在接收文件的適配器中設(shè)置接收目的地.
控制器:
public function indexAction(){ // action body $eform = new Application_Form_Eform(); if ($this->_request->isPost()) { $formData = $this->_request->getPost(); if ($eform->isValid($formData)) { $nameInput = $eform->getValue('email'); //receiving the file: $adapter = new Zend_File_Transfer_Adapter_Http(); $files = $adapter->getFileInfo(); // You should know the base path on server where you need to store the file. // You can put the server local address in Zend Registry in bootstrap, // then have it here like: Zend_Registry::get('configs')->...->basepath or something like that. // I just assume you will fix it later: $basePath = "/your_local_address/a_folder_for_unsafe_files/"; foreach ($files as $file => $info) { // set the destination on server: $adapter->setDestination($basePath, $file); // sometimes it would be a good idea to rename the file. // I left it for you to do it here: $fileName = $info['name']; $adapter->addFilter('Rename', array('target' => $fileName), $file); $adapter->receive($file); // You can make sure of having the file: if (file_exists($filePath . $fileName)) { // you can move, copy or change the file before redirecting to the new page. // Or you might need to keep the track of files and email in a database. } else { // throw error if you want. } } } // Then redirect: $this->_helper->redirector->gotoRouteAndExit (array( 'controller' => 'index', 'action' =>'result', 'email' => $nameInput)); } $this->view->form = $eform;}
來源:https://www.icode9.com/content-1-534501.html