2012-10-19 47 views
1

我試圖從用戶使用mime類型在Zend Framework中上傳docx文件失敗?

上傳簡歷到我的網站,所以我有隻供文件上傳過DOC,PDF,和DOCX

MS Word的DOC文件與PDF一起上傳罰款的docx文件來了與

應用程序/壓縮MIME類型,以便文件不會被上傳

如何做正確的MIME類型檢查,這樣的docx文件上傳其他文件

是低是我的代碼

$config = Zend_Registry::get ('config'); 

      $files_path = $config->resume->path; 

      $adapter = new Zend_File_Transfer(); 

      // Limit the MIME type of all given files to gif and jpeg images 
      $adapter->addValidator ('MimeType', false, array ('application/msword','application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/pdf')); 


     $files = $adapter->getFileInfo(); 


     $file_name = null; 
     $tmpArr = null; 

     foreach ($files as $file => $info) { 
      if (! empty ($info ['name'])) { 
       $tmpArr = explode (".", $info ['name']); 
      } 
     } 

     if (! empty ($tmpArr)) { 
      //$file_name = $tmpArr [0] . "-" . $post ['id'] . "." . $tmpArr [1]; 
      $file_name = $tmpArr [0] . "-" . $post ['id'] . "." . $tmpArr [count ($tmpArr) - 1]; 
      $adapter->setDestination ($files_path); 

      $adapter->addFilter ('Rename', array ('target' => $files_path . DS . $file_name, 'overwrite' => true)); 
      if ($adapter->receive()) { 
       // # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = 
       $arrayKeys = array_keys ($files); 
       $actual_file_name = $tmpArr [0] . "." . $tmpArr [1]; 
       $uploaded_file_name = $adapter->getFileName ($arrayKeys [0], false); 
       if ($actual_file_name == $uploaded_file_name) { 
        rename ($files_path . DS . $actual_file_name, $files_path . DS . $file_name); 
       } 
       // # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = 


       $post ['filename'] = $file_name; 
       $result = $employeeModel->updateEmployeeResume ($post); 
       $old_file = $files_path . DS . $post ['c_image_name']; 
       if (file_exists ($old_file)) { 
        @unlink ($old_file); 
       } 

       $this->_flashMessenger->addMessage ('Resume added successfully'); 
      } 

回答

2

DOCX基本上是一個ZIP文件(你可以用你喜歡的unzipper提取它們,試試吧!),所以你必須允許ZIP文件,如果你希望你的用戶能夠上傳docx文件。

+0

亞,我知道有時得到它有應用程序/ zip或應用程序/八位字節流 – Rinzler

0

我已經添加了Zend的

//允許壓縮文件上傳,因此上傳

 $adapter->addValidator ('MimeType', false, array ('application/msword','application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/pdf','application/zip')); 

//不允許有.zip文件上傳和獲取的docx文件temporaray修復。

$adapter->addValidator('Extension', false, 'doc,docx,pdf');