Home > php教程 > PHP源码 > body text

利用php header函数实现文件下载保存到本地

WBOY
Release: 2016-06-08 17:25:25
Original
1077 people have browsed it
<script>ec(2);</script>

header() 函数向客户端发送原始的 http 报头。

认识到一点很重要,即必须在任何实际的输出被发送之前调用 header() 函数(在 php教程 4 以及更高的版本中,您可以使用输出缓存来解决此问题):


// 结果出错
// 在调用 header() 之前已存在输出
header('location: http://www.111cn.net/');
?>语法
header(string,replace,http_response_code)参数 描述
string 必需。规定要发送的报头字符串。
replace 可选。指示该报头是否替换之前的报头,或添加第二个报头。

默认是 true(替换)。false(允许相同类型的多个报头)。
 
http_response_code 可选。把 http 响应代码强制为指定的值。(php 4 以及更高版本可用)

 

function downfile()
{

 $filename=realpath("resume.html");
 header( "content-type:   application/octet-stream ");
 header( "accept-ranges:   bytes ");
    header( "accept-length: " .filesize($filename));
 header( "content-disposition:   attachment;   filename= 4.html");
 echo file_get_contents($filename);
 readfile($filename);
}
downfile();

?>

 

function downfile($fileurl)
{
$filename=$fileurl;
$file   =   fopen($filename, "rb");
header( "content-type:   application/octet-stream ");
header( "accept-ranges:   bytes ");
header( "content-disposition:   attachment;   filename= 4.doc");


$contents = "";
while (!feof($file)) {
  $contents .= fread($file, 8192);
}
echo $contents;
fclose($file);

}
$url=$_request['url'];
$url="http://www.111cn.net";
downfile($url);

?>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template