头文件强制下载tar档案 [英] headers to force the download of a tar archive
本文介绍了头文件强制下载tar档案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在服务器上有一个tar档案,必须通过php下载。这是我用过的代码:
i have a tar archive on the server that must be downloadable through php. This is the code that i've used:
$content=file_get_contents($tar);
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=$tar");
header("Content-Length: ".strlen($content));
unlink($name);
die($content);
文件已下载但已损坏,无法打开。我认为头文件有问题,因为服务器上的文件可以毫无问题地打开。你知道我该如何解决这个问题吗?
The file is downloaded but it's broken and it can't be open. I think that there's something wrong with headers because the file on the server can be open without problems. Do you know how can i solve this problem?
更新
我尝试打印一个iframe,像这样:
UPDATE I've tried to print an iframe like this:
<iframe src="<?php echo $tar?>"></iframe>
下载工作,所以我确定头文件中缺少某些内容。
And the download works, so i'm sure that there's something missing in headers.
推荐答案
当我必须这样做时,我已经使用了这段代码:
I have used this code when I have had to do it:
function _Download($f_location, $f_name){
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($f_location));
header('Content-Disposition: attachment; filename=' . basename($f_name));
readfile($f_location);
}
_Download("../directory/to/tar/raj.tar", "raj.tar");
//or
_Download("/var/www/vhost/domain.com/httpdocs/directory/to/tar/raj.tar", "raj.tar");
试试看。
这篇关于头文件强制下载tar档案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文