相关疑难解决方法(0)

文件下载的HTTP标头

我编写了一个PHP脚本来处理文件下载,确定正在请求的文件,并设置正确的HTTP头以触发浏览器实际下载文件(而不是在浏览器中显示).

我现在遇到一个问题,其中一些用户报告某些文件被错误识别(因此无论扩展名如何,浏览器都会将其视为GIF图像).我猜这是因为我没有在响应头中设置"Content-type".这种情况最有可能发生吗?如果是这样,是否有一个可以用于所有文件的相当通用的类型,而不是试图考虑每种可能的文件类型?

目前我只设置值"Content-disposition:attachment; filename = arandomf.ile"

更新:我在此处按照本指南构建了一个更强大的文件下载过程(http://w-shadow.com/blog/2007/08/12/how-to-force-file-download-with-php/) ,但是在执行脚本和显示浏览器的下载对话框之间存在明显的延迟.任何人都可以找出造成这种情况的瓶颈吗?

这是我的实现:

/**
 * Outputs the specified file to the browser.
 *
 * @param string $filePath the path to the file to output
 * @param string $fileName the name of the file
 * @param string $mimeType the type of file
 */
function outputFile($filePath, $fileName, $mimeType = '') {
    // Setup
    $mimeTypes = array(
        'pdf' => 'application/pdf',
        'txt' => 'text/plain',
        'html' => 'text/html',
        'exe' => 'application/octet-stream',
        'zip' => 'application/zip',
        'doc' => …
Run Code Online (Sandbox Code Playgroud)

php content-type download http-headers

50
推荐指数
3
解决办法
15万
查看次数

标签 统计

content-type ×1

download ×1

http-headers ×1

php ×1