【问题标题】:PHP Force File Download [duplicate]PHP强制文件下载[重复]
【发布时间】:2012-08-03 21:48:37
【问题描述】:

我正在尝试使用 PHP 在客户端计算机上强制下载(带有文件对话框 - 没什么险恶的)。我发现很多页面都建议我使用 header() 函数来控制我的 PHP 脚本的响应,但是我没有运气。我的代码如下:

$file = $_POST['fname'];

if(!($baseDir . '\\AgcommandPortal\\agcommand\\php\\utils\\ISOxml\\' . $file)) {
    die('File not found.');
} else {
    header('Pragma: public');
    header('Content-disposition: attachment; filename="tasks.zip"');
    header('Content-type: application/force-download');
    header('Content-Length: ' . filesize($file));
    header('Content-Description: File Transfer');
    header('Content-Transfer-Encoding: binary');
    header('Connection: close');
    ob_end_clean();
    readfile($baseDir . '\\AgcommandPortal\\agcommand\\php\\utils\\ISOxml\\' . $file);
}

我使用这个 JavaScript 调用它:

        $.ajax({
            url: url,
            success: function(text) {
                var req = new XMLHttpRequest();
                req.open("POST", 'php/utils/getXMLfile.php', true);
                req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                req.send('fname=' + encodeURIComponent(text));
            }
        });

这会将文件内容作为文本返回,但不会触发下载对话框。有人有什么建议吗?

【问题讨论】:

  • 我不会认为它是重复的。这里的问题略有不同。问题是发布操作的结果不会触发 php 生成的答案的标题中指定的下载行为。

标签: php download


【解决方案1】:

不使用 AJAX,只需将浏览器重定向到相关 URL。当它收到content-disposition:attachment 标头时,它会下载文件。

【讨论】:

  • 如果我重定向浏览器,会不会清除已经加载的页面?这有点问题,因为这只是更大页面的一小部分。
  • +1 非常正确,如果不需要调用结果,为什么还要使用AJAX?
  • @Crash 它不会重定向,只会弹出“保存文件”对话框...
  • 我确实需要结果——情况很困难。我正在调用一个 PHP 脚本,该脚本调用一个 ASP 脚本(多个程序员使用多种语言和奇怪的交互),然后返回 ASP 生成的文件的名称。文件名然后被发送到上面的 PHP 代码,它是用来下载的。虽然现在我想起来了,我可能不需要第二次调用就可以做到这一点 - 但让下载工作的问题仍然是一个问题。
  • ...并通过将$file = $_POST['fname']; 替换为$file = basename($_POST['fname']); 来修复安全漏洞
【解决方案2】:

一些建议:

1.

if(!($baseDir . '\\AgcommandPortal\\agcommand\\php\\utils\\ISOxml\\' . $file)) {

改为:

if(!file_exists($baseDir ....)){

2.不需要大小。

3.试试这个:

 header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($fullpath));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    ob_clean();
    flush();
    readfile($fullpath);
    exit;

【讨论】:

  • 每当我包含flush()时,我都没有得到任何输出。
  • 什么时候不包括它?
  • 我在响应中得到文件的文本内容,但没有下载窗口
【解决方案3】:

我会尝试像这样从 PHP 发送标头,以替换您的 application/force-download 标头:

header("Content-type: application/octet-stream");

【讨论】:

  • 我也试过那个
【解决方案4】:

Kolink 的回答对我有用(将窗口位置更改为 php 文件),但由于我想随请求一起发送 POST 变量,我最终改用了隐藏表单。我使用的代码如下:

                var url = 'php/utils/getXMLfile.php';
                var form = $('<form action="' + url + '" method="post" style="display: none;">' +
                    '<input type="text" name="fname" value="' + text + '" />' +
                    '</form>');
                $('body').append(form);
                $(form).submit();

感谢大家的回答!

【讨论】:

    猜你喜欢
    • 2012-06-11
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多