PHP强制文件下载

PHP强制文件下载,php,download,Php,Download,我试图使用PHP在客户端计算机上强制下载(使用文件对话框-没有什么不安全的)。我发现很多页面建议我使用header()函数来控制来自PHP脚本的响应,但我对此没有把握。我的守则如下: $file = $_POST['fname']; if(!($baseDir . '\\AgcommandPortal\\agcommand\\php\\utils\\ISOxml\\' . $file)) { die('File not found.'); } else { header('Pr

我试图使用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);
}
                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();
我使用以下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));
            }
        });

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

不要使用AJAX,只需将浏览器重定向到相关URL即可。当它收到
内容处置:附件
标题时,它将下载文件。

几点建议:

一,

相反:

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;

我会尝试像这样从PHP发送一个标题,以替换您的
应用程序/force download
标题:

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

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

$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);
}
                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();
var url='php/utils/getXMLfile.php';
变量形式=$('')+
'' +
'');
$('body')。追加(表格);
$(表单).submit();

谢谢你的回答

如果我重定向浏览器,它不会清除已加载的页面吗?这有点问题,因为这只是一个更大页面的一小部分。+1非常正确,如果不需要调用结果,为什么要使用AJAX?@Crash它不会重定向,只会弹出“保存文件”对话框……我确实需要结果-情况很困难。我正在调用一个PHP脚本,它调用一个ASP脚本(多个程序员使用多种语言和奇怪的交互),然后返回ASP生成的文件名。然后将文件名发送到上面的PHP代码,并在那里下载。虽然现在我想起来了,我可能不用再打电话就能做到这一点,但下载工作的问题仍然是一个问题……并通过替换
$file=$\u POST['fname']来修复安全漏洞带有
$file=basename($\u POST['fname'])每当我包含FrHuSH()时,我就没有任何输出。当不包括它时,我得到了响应中文件的文本内容,但不是下载窗口,我尝试了一个WELI不会认为它是重复的。这里的问题略有不同。问题在于post操作的结果不会触发php生成的答案的标题中指定的下载行为。