2012-10-19 116 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');