使用 header()强制下载 php 文件

我希望用户能够下载一些文件,我有在我的服务器上,但当我试图使用这周围的互联网的许多例子,似乎没有工作为我。我试过这样的代码:

<?php


$size = filesize("Image.png");


header('Content-Description: File Transfer');
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename="Image.png"');
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: ' . $size);
readfile("Image.png");

我甚至尝试用我能找到的最基本的例子,比如:

<?php
header('Content-type: image/png');
header('Content-Disposition: attachment; filename="Image.png"');
readfile('Image.png');

当我测试这一点,我已经删除了所有其他代码,我已经使用一个空文件,只有这个代码,以消除任何由外部来源创建的错误。

当我在控制台中查看文件时,会发送正确的头文件,即

'Content-Disposition: attachment; filename="Image.png"'

但是没有显示保存对话框。

我也试过在内联而不是在内容处理标题中使用附件,但这也没有什么区别,我在 Firefox 8.0.1 Chrome 15.0.874.121和 Safari 5.1.1中测试过。

226526 次浏览

我很确定你不会在文件下载时添加哑剧类型作为 JPEG:

header('Content-Type: image/png');

这些标题从未让我失望:

$quoted = sprintf('"%s"', addcslashes(basename($file), '"\\'));
$size   = filesize($file);


header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);

问题是,当我使用一个直接链接下载文件时,我使用 ajax 将消息发送到服务器,一切正常。

我使用了另外一个 Stackoverflow 的问答材料,它对我很有效:

对我来说很有用

$attachment_location = "filePath";
if (file_exists($attachment_location)) {


header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header("Cache-Control: public"); // needed for internet explorer
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: Binary");
header("Content-Length:".filesize($attachment_location));
header("Content-Disposition: attachment; filename=filePath");
readfile($attachment_location);
die();
} else {
die("Error: File not found.");
}

这对我来说就像下载 PNG 和 PDF 文件一样有效。

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$file_name.'"');
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_url)); //Absolute URL
ob_clean();
flush();
readfile($file_url); //Absolute URL
exit();

基于 Farhan Sahibole 的回答:

        header('Content-Disposition: attachment; filename=Image.png');
header('Content-Type: application/octet-stream'); // Downloading on Android might fail without this
ob_clean();


readfile($file);

我只需要这个就行了,我脱掉了所有不需要的东西。

关键是使用 ob_clean();

Here is a snippet from me in testing... obviously passing via get to the script may not be the best... should post or just send an id and grab guid from db... anyhow.. this worked. I take the URL and convert it to a path.

// Initialize a file URL to the variable
$file = $_GET['url'];
$file = str_replace(Polepluginforrvms_Plugin::$install_url, $DOC_ROOT.'/pole-permitter/', $file );


$quoted = sprintf('"%s"', addcslashes(basename($file), '"\\'));
$size   = filesize($file);


header( "Content-type: application/octet-stream" );
header( "Content-Disposition: attachment; filename={$quoted}" );
header( "Content-length: " . $size );
header( "Pragma: no-cache" );
header( "Expires: 0" );
readfile( "{$file}" );

Htaccess 解决方案

<filesmatch "\.(?i:doc|odf|pdf|cer|txt)$">
Header set Content-Disposition attachment
</FilesMatch>

你可以阅读以下内容: Https://www.techmesto.com/force-files-to-download-using-htaccess/