2014-01-17 107 views
3

我有一個簡單的表單,它有上傳的電子郵件和文件,它顯示正確,提交後它轉到我設置的結果頁面,正確。 但是,我無法在服務器上找到現在的文件。如何在Zend中實現文件上傳?

這裏是我的代碼:

形式:

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

    } 

我到底做錯了什麼?由於

回答

1

在控制器,你需要一個適配器來接收文件。 Zend_File_Transfer_Adapter_Http是一個簡單的解決方案。您應該在接收文件的適配器中設置接收目標。

控制器:

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

呦應包括,

$file->setDestination(BASE_PATH . '/public/files') // the path you want to set 

在你的形式,

$file = new Zend_Form_Element_File('file'); 
     $file->setLabel('Upload CSV file:') 
      ->setDestination(BASE_PATH . '/public/files') 
      ->setRequired(true) 
      ->addValidator('NotEmpty') 
      ->addValidator('Count', false, 1); 
     $this->addElement($file); 
如上

..在控制器,你可以做如下

if ($this->_request->isPost()) { 
     $formData = $this->_request->getPost(); 
      if ($form->isValid($formData)) {  
      $uploadedData = $form->getValues(); 
      Zend_Debug::dump($uploadedData, '$uploadedData'); 
      Zend_Debug::dump($fullFilePath, '$fullFilePath'); //[for file path] 
     } 
    } 

你應該定義th E採用在bootstrap.php中

if(!defined('BASE_PATH')) { 
    define('BASE_PATH', dirname(__FILE__) . '/..'); 
} 

使用這個例子更多的理解, ZEND FILE UPLOAD

改變你的形式到這一點,

class forms_UploadForm extends Zend_Form 
{ 
    public function __construct($options = null) 
    { 
     parent::__construct($options); 
     $this->setName('upload'); 
     $this->setAttrib('enctype', 'multipart/form-data'); 

     $description = new Zend_Form_Element_Text('email'); 
     $description->setLabel('Email') 
        ->setRequired(true) 
        ->addValidator('NotEmpty'); 

     $file = new Zend_Form_Element_File('file'); 
     $file->setLabel('File') 
      ->setDestination(BASE_PATH . '/files') 
      ->setRequired(true); 

     $submit = new Zend_Form_Element_Submit('submit'); 
     $submit->setLabel('Upload'); 

     $this->addElements(array($description, $file, $submit)); 

    } 
} 
+0

凡在bootstrap.php中? – Liza

+0

如果你喜歡 –

+0

是的,它是爲Zend 1.12,請打開你的錯誤報告,並確保你有一個公共文件夾中的文件目錄,或者你正在給的任何路徑存在。 –