我的服务器上有一个 tar 存档,必须可以通过 php 下载。这是我使用过的代码:
$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);
文件已下载,但已损坏且无法打开。我认为标头有问题,因为服务器上的文件可以毫无问题地打开。你知道我该如何解决这个问题吗?
UPDATE我尝试打印这样的 iframe:
<iframe src="<?php echo $tar?>"></iframe>
并且下载有效,所以我确信标头中缺少某些内容。
当我不得不这样做时,我使用了这段代码:
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");
尝试一下。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)