通过 Php 提供 .docx 文件
我在尝试使用 Php 提供 .docx 文件时遇到问题。上传文件时,我检测文件 mime 类型,并根据 mime 类型使用具有正确扩展名的文件上传文件;例如,如下:
application/msword - doc
application/vnd.openxmlformats-officedocument.wordprocessingml.document - docx
当尝试提供文件下载时,我会执行相反的操作,检测扩展名并根据 mime 类型提供服务,例如
public static function fileMimeType($extention) {
if(!is_null($extention)) {
switch($extention) {
case 'txt':
return 'text/plain';
break;
case 'odt':
return 'application/vnd.oasis.opendocument.text';
break;
case 'doc':
return 'application/msword';
break;
case 'docx':
return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
break;
case 'jpg':
return 'image/jpeg';
break;
case 'png':
return 'image/png';
break;
case 'pdf':
return 'application/pdf';
break;
default:
break;
}
}
}
所有文件似乎都能正确下载并正常打开,但是当尝试打开 docx 文件时,Word(在多个files) 抛出一个错误,指出文件已损坏。
任何想法都会很棒,谢谢。
编辑 #1
try {
$file = new Booking_Document((int)$get_data['bookingDocument']);
header('Content-Type: ' . Booking_Document::fileMimeType($file->getDocumentType()));
header('Content-Disposition: attachment; filename=' . $file);
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
echo readfile(Zend_Registry::get(static::$_uploadDir).$this->_id);
} catch (Exception $e) {
View_Helpers_FlashMessages::addMessage(array('message' => $e->getMessage(), 'type' => 'error'));
}
exit;
已修复
在调用 readfile() 之前,我添加了 ob_clean() 和lush(),这似乎已经解决了问题。
I'm having issues when attempting to serve a .docx file using Php. When uploading the file I detect the file mime type and upload the file using the file with the correct extension based on the mime type; e.g. below:
application/msword - doc
application/vnd.openxmlformats-officedocument.wordprocessingml.document - docx
When attempting to serve the files for download, I do the reverse in detecting the extension and serving based on the mime type e.g.
public static function fileMimeType($extention) {
if(!is_null($extention)) {
switch($extention) {
case 'txt':
return 'text/plain';
break;
case 'odt':
return 'application/vnd.oasis.opendocument.text';
break;
case 'doc':
return 'application/msword';
break;
case 'docx':
return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
break;
case 'jpg':
return 'image/jpeg';
break;
case 'png':
return 'image/png';
break;
case 'pdf':
return 'application/pdf';
break;
default:
break;
}
}
}
All files appear to download correctly and open fine but when attempting to open a docx file, Word (on multiple files) throws a error stating the file is corrupt.
Any ideas would be great, thanks.
Edit #1
try {
$file = new Booking_Document((int)$get_data['bookingDocument']);
header('Content-Type: ' . Booking_Document::fileMimeType($file->getDocumentType()));
header('Content-Disposition: attachment; filename=' . $file);
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
echo readfile(Zend_Registry::get(static::$_uploadDir).$this->_id);
} catch (Exception $e) {
View_Helpers_FlashMessages::addMessage(array('message' => $e->getMessage(), 'type' => 'error'));
}
exit;
FIXED
Prior to calling readfile() I added ob_clean() and flush() which appears to have fixed the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
固定的;在调用readfile()之前,我添加了ob_clean()和flush(),这似乎已经解决了问题。
Fixed; prior to calling readfile() I added ob_clean() and flush() which appears to have fixed the problem.
几天前我也遇到了类似的问题。这是由于在读取文件之前输出了一些字符。这些字符被插入到下载的文件的开头,使得当我尝试打开它时它看起来已损坏(在本例中为 PDF)。
I had a similar problem some days ago. It was due to some characters being output just before the file was read. These chars were inserted at the beginning of the downloaded file, making it appear as corrupted when I tried to open it (PDF in this case).