设置内容类型标题前的PHP回显?
我有以下PHP代码:设置内容类型标题前的PHP回显?,php,http-headers,Php,Http Headers,我有以下PHP代码: function download_file($file, $description=null, $filename=false){ $filename = $filename || basename($file); ob_start(); if(is_string($description)){ $description = preg_replace("/\$1/", $filename, $description);
function download_file($file, $description=null, $filename=false){
$filename = $filename || basename($file);
ob_start();
if(is_string($description)){
$description = preg_replace("/\$1/", $filename, $description);
echo $description;
}
sleep(2);
header('Content-Type: '.$this->file_mimetype($file));
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file) . "\"");
readfile($file);
header("Content-Type: text/html");
ob_end_flush();
}
download_file("https://raw.githubusercontent.com/Gethis/ED/master/easydevop.class.php", "Downloading easydevop.class.php");
问题是它在下载之前没有响应“Downloading easydevop.class.php”。我也试着在所有的标题后重复它,但那也不起作用。求求你,有什么帮助吗
正如您所见,我确实使用了ob\u start()
和ob\u end\u flush()
您不能使用“echo”(显示HTML内容)同时向用户发送文件。
您可以先显示HTML页面,然后使用
HTML重定向
<META HTTP-EQUIV="REFRESH" CONTENT="0;URL=http://url.to/file/or_script/that_send_file/">
或者javascript重定向,正如我已经提到的,下载文件时不能显示echo。当你们下载文件时,你们只需要下载文件,并没有更多 不过,使用JavaScript,您可以在开始下载之前显示消息。以下是测试脚本:
<?php
if (isset($_GET['id'])) {
$file = 'testfile.txt';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<script type="text/javascript">
function showDownload(message) {
document.getElementById("hidden").innerHTML = message;
document.getElementById("link1").style.display = 'none'; // you can even hide download link if you want
}
</script>
<div id="hidden"></div>
<a href="download.php?id=1" onclick="showDownload('You are downloading file id = 1'); return true;" id="link1">Download</a>
</body>
</html>
功能显示下载(信息){
document.getElementById(“隐藏”).innerHTML=消息;
document.getElementById(“link1”).style.display='none';//如果需要,甚至可以隐藏下载链接
}