在 Zend Framework 中使用 mime 类型上传 docx 文件失败?
•浏览 1
Uploading docx file in Zend Framework with mime type fails?
我正在尝试将用户的简历上传到我的网站
所以我也限制了文件上传 doc、pdf 和 docx
ms word doc 文件与 pdf 一起上传,但 docx 文件与
一起上传
application/zip 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' );
}
$adapter->addValidator ( 'MimeType', false, array ('application/msword','application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/pdf','application/zip' ) );
$adapter->addValidator('Extension', false, 'doc,docx,pdf');
DOCX 基本上是一个 ZIP 文件(你可以用你最喜欢的解压器解压它们,试试看!),所以如果你希望你的用户能够上传 DOCX 文件,你必须允许 ZIP 文件。
我在zend
中添加了一个临时修复
// 允许上传 zip 文件
$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' );
}
$adapter->addValidator ( 'MimeType', false, array ('application/msword','application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/pdf','application/zip' ) );
$adapter->addValidator('Extension', false, 'doc,docx,pdf');
// 禁止上传带有 .zip 的文件并因此上传 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' );
}
$adapter->addValidator ( 'MimeType', false, array ('application/msword','application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/pdf','application/zip' ) );
$adapter->addValidator('Extension', false, 'doc,docx,pdf');