通过 Php 提供 .docx 文件

发布于 2024-10-10 13:49:09 字数 2054 浏览 5 评论 0原文

我在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

じ违心 2024-10-17 13:49:09

固定的;在调用readfile()之前,我添加了ob_clean()和flush(),这似乎已经解决了问题。

Fixed; prior to calling readfile() I added ob_clean() and flush() which appears to have fixed the problem.

吃素的狼 2024-10-17 13:49:09

几天前我也遇到了类似的问题。这是由于在读取文件之前输出了一些字符。这些字符被插入到下载的文件的开头,使得当我尝试打开它时它看起来已损坏(在本例中为 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文