Home >Backend Development >PHP Tutorial >php用header()实现文件下载功能

php用header()实现文件下载功能

WBOY
WBOYOriginal
2016-06-20 13:01:461420browse

php用header()实现文件下载功能代码:

<p>function download($file_url,$new_name=''){</p>	if(!isset($file_url)||trim($file_url)==''){<br />		return '500';<br />	}<br />	if(!file_exists($file_url)){//检查文件是否存在<br />		return '404';<br />	}<br />	$file_name=basename($file_url);<br />	$file_type=explode('.',$file_url);<br />	$file_type=$file_type[count($file_type)-1];<br />	$file_name=trim($new_name=='')?$file_name:urlencode($new_name).'.'.$file_type;<br />	//输入文件标签phpernote<br />	header("Content-type: application/octet-stream");<br />	header("Accept-Ranges: bytes");<br />	header("Accept-Length: ".filesize($file_url));<br />	header("Content-Disposition: attachment; filename=".$file_name);<br />	//输出文件内容<br />	@readfile($file_type);<br />}


Statement:
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