【问题标题】:Headers used to download file php [duplicate]用于下载文件php的标头[重复]
【发布时间】:2012-11-10 21:27:30
【问题描述】:

可能重复:
php, file download

我有一些不在 Web 根目录中的文件,我需要将其提供给下载。所以我有一个脚本,它使用下面的来下载请求的文件。问题是,我下载的每个文件都损坏了?文件没问题,因为如果我使用 FTP 下载,它们就会打开。以下是传递的标头:

header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
        header("Cache-Control: public"); // needed for i.e.
        header("Content-Type: " . $download[0]['mime']);
        header("Content-Disposition: attachment; filename=" .$download_file);
        header("Content-Transfer-Encoding: Binary");
        header("Content-Length:".filesize($attachment_location));

        readfile($attachment_location);

【问题讨论】:

    标签: php header download


    【解决方案1】:

    以下是用于此的标头示例:
    http://php.net/manual/en/function.readfile.php

        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
    

    【讨论】:

    • 谢谢你,这是一种享受,我想我需要将 mime 类型设置为内容类型
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    相关资源
    最近更新 更多