把下面的代码存为down.php,在同目录下建一个 1.txt文件,用http://localhost/down.php?file=1.txt来访问就可以下载1.txt这个文件了.<?
function get_ext_name($file){
$part= explode('.',$file );
$ext = $part[count($part) - 1];
return ($ext);
}
function read_from_file ( $file ){
if ( ! file_exists ( $file ) ){
return ( FALSE );
}
$fp = fopen ( $file, "r" );
if ( ! $fp ){
return ( FALSE );
}
flock ( $fp, LOCK_SH );
$data = fread ( $fp, filesize ( $file ) );
fclose ( $fp );
return ( $data );
}
$filename= basename($file);
$ext = get_ext_name($filename);
$mime_type=(USR_BROWSER_AGENT=='IE'||USR_BROWSER_AGENT=='OPERA')?'application/octetstream':'application/octet-stream';header ('Content-Type:'.$mime_type);
$content_disp =(USR_BROWSER_AGENT =='IE')?'inline':'attachment';
header ('Content-Disposition:'.$content_disp.';filename="'.$filename.'"');
header ('Pragma: no-cache');
header ('Expires: 0');
$content =read_from_file($file);
echo $content;
?>