【问题标题】:Using Header as download link in PHP在 PHP 中使用 Header 作为下载链接
【发布时间】:2011-03-29 14:38:31
【问题描述】:

所以我的下载链接遇到了这个问题。基本上我以前创建下载链接的方法是只有一个带有 method='link' 的表单按钮和作为文件链接的操作。这适用于 firefox 和其他人,但不适用于 safari。出于某种原因,当用户尝试从 safari 下载文件(excel 文件)时,它只会在浏览器上显示一堆 ascii 字符(我猜它试图使用浏览器读取它?)。好吧,我一直在寻找另一种解决方案,似乎使用标头是这样做的方法。所以现在我正在尝试使用 method='post' 和 action='download.php' 创建一个表单按钮,其中有一个带有文件链接的隐藏字段。看起来是这样的

function showDownloadWithHeader($link){
    echo "<form action='download.php' method='post' >";
    echo "<input class='downloadButton' type='submit' value='Download Report'>";
    echo "<input type='hidden' name='filename' value='$link'>";
    echo "</form>";
}

在 download.php 中,我只想要求用户下载文件。

<?php
    if($_POST['filename'] == '' || empty($_POST['filename'])){
        exit;
    }

    $filename = $_POST['filename']; //the file is 2 folder down. e.g. data/stack/bla.xlsx
    $file = $filename;
    $extension = end(explode('.', $filename));
    error_reporting(E_ALL);
    ini_set("display_errors",1);
//  echo $filename;
//  echo "<br/>";
//  echo $extension;
//  echo filesize($filename);
//  echo "<br/>";
    switch($extension){
        case 'xls':
            $mimeType = 'application/vnd.ms-excel';
            break;
        case 'xlsx':
            $mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
            break;
    }
//  echo $mimeType;
    header('Content-Description: File Transfer');
    header('Content-Type: ' . $mimeType);
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($filename);
    exit;
?>

我在 php.net 上的 readfile() 函数下看到了这个解决方案,但它似乎对我不起作用。我在 localhost 上执行此操作。

【问题讨论】:

  • 它以什么方式“不起作用”?有什么错误吗?文件是否存在?你说“文件是 2 个文件夹”,但你使用的是 $filename 原样?
  • $filename 已经考虑到 2 文件夹了。 $filename = 'data/orange/orange.xlsx' 它只是向我显示一个空白页面,而不是要求我下载文件。

标签: php header http-headers


【解决方案1】:

这样的东西很好用。

header("Pragma: public", true);
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
die(file_get_contents($file));

【讨论】:

  • 什么是应用程序/强制下载和应用程序/下载?它们不是有效的常见 mime 类型,它们可能会在某些浏览器/程序中使用,但通常不会
  • 它们可能是特定于浏览器的标头。这个脚本在我的图书馆里已经有一段时间了 - 它还没有让我失望。
  • @Mitch C:出于某种原因,您的脚本仅适用于 safari。当尝试使用 firefox 打开它时,脚本将尝试在没有 excel 扩展名的情况下保护文件。
  • header("Content-Disposition: attachment; filename=".basename($file).".csv");
  • 引用文件名 filename="myfile.xlsx"
【解决方案2】:

如果我理解正确,脚本会按预期工作,即如果浏览器识别 mime 类型:

'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'

然后它在浏览器中显示它(它是否正确显示是另一回事)

既然你想强制它下载而不是在浏览器中显示,请使用 mime 类型:

'application/octet-stream'

这应该适用于所有浏览器并强制下载而不是在浏览器中显示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2011-12-24
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多