功能:
从远程下载文件。
代码:
/**
* 远程下载文件
* edit www.
*/
if ($_GET[xfer]) {
if ($_POST[from] == "") {
print "You forgot to enter a url.";
} else {
copy("$_POST[from]", "$_POST[to]");
$size = round((filesize($_POST[to])/1000000), 3);
print "transfer complete.<br>
<a><a href=/index.html"$_POST[from]\">$_POST[from]</a><br>
<a><a href=/index.html"$_POST[to]\">$_POST[to]</a> : $size MB";
}
} else {
print "<form action=\"$PHP_SELF?xfer=true\" method=post>
from(http://): <input name=from><br>
to(filename): <input name=to><br>
<input type=submit value=\"transload\">";
}
?>
代码如下:
<?
/**
** php文件下载类
**范例:
$download=new download('php,exe,html',false);
if(!$download->downloadfile($filename))
{
echo $download->geterrormsg();
}
* by www.
*/
class download{
var $debug=true;
var $errormsg='';
var $Filter=array();
var $filename='';
var $mineType='text/plain';
var $xlq_filetype=array();
function download($fileFilter='',$isdebug=true)
{
$this->setFilter($fileFilter);
$this->setdebug($isdebug);
$this->setfiletype();
}
function setFilter($fileFilter)
{
if(empty($fileFilter)) return ;
$this->Filter=explode(',',strtolower()($fileFilter));
}
function setdebug($debug)
{
$this->debug=$debug;
}
function setfilename($filename)
{
$this->filename=$filename;
}
function downloadfile($filename)
{
$this->setfilename($filename);
if($this->filecheck())
{
$fn = array_pop( explode()( '/', strtr( $this->filename, '\\', '/' ) ) );
header( "Pragma: public" );
header( "Expires: 0" ); // set expiration time
header( "Cache-Component: must-revalidate, post-check=0, pre-check=0" );
header( "Content-type:".$this->mineType );
header( "Content-Length: " . filesize( $this->filename ) );
header( "Content-Disposition: attachment; filename=\"$fn\"" );
header( 'Content-Transfer-Encoding: binary' );
readfile( $this->filename );
return true;
}else
{
return false;
}
}
function geterrormsg()
{
return $this->errormsg;
}
function filecheck()
{
$filename=$this->filename;
if(file_exists($filename))
{
$filetype=strtolower(array_pop(explode('.',$filename)));
if(in_array($filetype,$this->Filter))
{
$this->errormsg.=$filename.'不允许下载!';
if($this->debug) exit($filename.'不允许下载!') ;
return false;
}else
{
if ( function_exists( "mime_content_type" ) )
{
$this->mineType = mime_content_type( $filename );
}
if(empty($this->mineType))
{
if( isset()($this->xlq_filetype[$filetype]) ) $this->mineType = $this->xlq_filetype[$filetype];
}
if(!empty($this->mineType))
return true;
else
{
$this->errormsg.='获取'.$filename.'文件类型时候发生错误,或者不存在预定文件类型内';
if($this->debug) exit('获取文件类型出错');
return false;
}
}
}else
{
$this->errormsg.=$filename.'不存在!';
if($this->debug) exit($filename.'不存在!') ;
return false;
}
}
function setfiletype()
{
$this->xlq_filetype['chm']='application/octet-stream';
$this->xlq_filetype['ppt']='application/vnd.ms-powerpoint';
$this->xlq_filetype['xls']='application/vnd.ms-excel';
$this->xlq_filetype['doc']='application/msword';
$this->xlq_filetype['exe']='application/octet-stream';
$this->xlq_filetype['rar']='application/octet-stream';
$this->xlq_filetype['js']="javascript/js";
$this->xlq_filetype['css']="text/css";
$this->xlq_filetype['hqx']="application/mac-binhex40";
$this->xlq_filetype['bin']="application/octet-stream";
$this->xlq_filetype['oda']="application/oda";
$this->xlq_filetype['pdf']="application/pdf";
$this->xlq_filetype['ai']="application/postsrcipt";
$this->xlq_filetype['eps']="application/postsrcipt";
$this->xlq_filetype['es']="application/postsrcipt";
$this->xlq_filetype['rtf']="application/rtf";
$this->xlq_filetype['mif']="application/x-mif";
$this->xlq_filetype['csh']="application/x-csh";
$this->xlq_filetype['dvi']="application/x-dvi";
$this->xlq_filetype['hdf']="application/x-hdf";
$this->xlq_filetype['nc']="application/x-netcdf";
$this->xlq_filetype['cdf']="application/x-netcdf";
$this->xlq_filetype['latex']="application/x-latex";
$this->xlq_filetype['ts']="application/x-troll-ts";
$this->xlq_filetype['src']="application/x-wais-source";
$this->xlq_filetype['zip']="application/zip";
$this->xlq_filetype['bcpio']="application/x-bcpio";
$this->xlq_filetype['cpio']="application/x-cpio";
$this->xlq_filetype['gtar']="application/x-gtar";
$this->xlq_filetype['shar']="application/x-shar";
$this->xlq_filetype['sv4cpio']="application/x-sv4cpio";
$this->xlq_filetype['sv4crc']="application/x-sv4crc";
$this->xlq_filetype['tar']="application/x-tar";
$this->xlq_filetype['ustar']="application/x-ustar";
$this->xlq_filetype['man']="application/x-troff-man";
$this->xlq_filetype['sh']="application/x-sh";
$this->xlq_filetype['tcl']="application/x-tcl";
$this->xlq_filetype['tex']="application/x-tex";
$this->xlq_filetype['texi']="application/x-texinfo";
$this->xlq_filetype['texinfo']="application/x-texinfo";
$this->xlq_filetype['t']="application/x-troff";
$this->xlq_filetype['tr']="application/x-troff";
$this->xlq_filetype['roff']="application/x-troff";
$this->xlq_filetype['shar']="application/x-shar";
$this->xlq_filetype['me']="application/x-troll-me";
$this->xlq_filetype['ts']="application/x-troll-ts";
$this->xlq_filetype['gif']="image/gif";
$this->xlq_filetype['jpeg']="image/pjpeg";
$this->xlq_filetype['jpg']="image/pjpeg";
$this->xlq_filetype['jpe']="image/pjpeg";
$this->xlq_filetype['ras']="image/x-cmu-raster";
$this->xlq_filetype['pbm']="image/x-portable-bitmap";
$this->xlq_filetype['ppm']="image/x-portable-pixmap";
$this->xlq_filetype['xbm']="image/x-xbitmap";
$this->xlq_filetype['xwd']="image/x-xwindowdump";
$this->xlq_filetype['ief']="image/ief";
$this->xlq_filetype['tif']="image/tiff";
$this->xlq_filetype['tiff']="image/tiff";
$this->xlq_filetype['pnm']="image/x-portable-anymap";
$this->xlq_filetype['pgm']="image/x-portable-graymap";
$this->xlq_filetype['rgb']="image/x-rgb";
$this->xlq_filetype['xpm']="image/x-xpixmap";
$this->xlq_filetype['txt']="text/plain";
$this->xlq_filetype['c']="text/plain";
$this->xlq_filetype['cc']="text/plain";
$this->xlq_filetype['h']="text/plain";
$this->xlq_filetype['html']="text/html";
$this->xlq_filetype['htm']="text/html";
$this->xlq_filetype['htl']="text/html";
$this->xlq_filetype['rtx']="text/richtext";
$this->xlq_filetype['etx']="text/x-setext";
$this->xlq_filetype['tsv']="text/tab-separated-values";
$this->xlq_filetype['mpeg']="video/mpeg";
$this->xlq_filetype['mpg']="video/mpeg";
$this->xlq_filetype['mpe']="video/mpeg";
$this->xlq_filetype['avi']="video/x-msvideo";
$this->xlq_filetype['qt']="video/quicktime";
$this->xlq_filetype['mov']="video/quicktime";
$this->xlq_filetype['moov']="video/quicktime";
$this->xlq_filetype['movie']="video/x-sgi-movie";
$this->xlq_filetype['au']="audio/basic";
$this->xlq_filetype['snd']="audio/basic";
$this->xlq_filetype['wav']="audio/x-wav";
$this->xlq_filetype['aif']="audio/x-aiff";
$this->xlq_filetype['aiff']="audio/x-aiff";
$this->xlq_filetype['aifc']="audio/x-aiff";
$this->xlq_filetype['swf']="application/x-shockwave-flash";
}
}
?>
浏览器发送一个请求,请求访问服务器中的某个网页(如:down.php),该网页的代码如下。
服务器接受到该请求以后,马上运行该down.php文件
运行该文件的时候,必然要把将要被下载的文件读入内存当中(这里是圣诞狂欢.jpg这张图片),这里通过fopen()函数完成该动作
注意:任何有关从服务器下载的文件操作,必然需要先在服务端将文件读入内存当中
现在文件已经在内存当中了,这是需要从内存当中读取文件,通过fread()函数完成该动作
注意:
如果文件较大,文件应该是被分成多段返回给客户端的,并不是等文件在服务端全部读取完毕后,一次性返回给客户端,因为会增加服务器的负荷。
因此,需要在php代码中设置一次读取的字节数,比如以下代码中通过$buffer=1024设置一次读取的字节数,每读取一次,就输出数据(即返回给浏览器)。
流程图:
header("Content-type:text/html;charset=utf-8");
// $file_name="cookie.jpg";
$file_name="圣诞狂欢.jpg";
//用以解决中文不能显示出来的问题
$file_name=iconv("utf-8","gb2312",$file_name);
$file_sub_path=$_SERVER['DOCUMENT_ROOT']."marcofly/phpstudy/down/down/";
$file_path=$file_sub_path.$file_name;
//首先要判断给定的文件存在与否
if(!file_exists($file_path)){
echo "没有该文件文件";
return ;
}
$fp=fopen($file_path,"r");
$file_size=filesize($file_path);
//下载文件需要用到的头
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length:".$file_size);
Header("Content-Disposition: attachment; filename=".$file_name);
$buffer=1024;
$file_count=0;
//向浏览器返回数据
while(!feof($fp) && $file_count<$file_size){
$file_con=fread($fp,$buffer);
$file_count+=$buffer;
echo $file_con;
}
fclose($fp);
?>
header("Content-type:text/html;charset=utf-8")的作用:在服务器响应浏览器的请求时,告诉浏览器以编码格式为UTF-8的编码显示该内容
关于file_exists()函数不支持中文路径的问题:因为php函数比较早,不支持中文,所以如果被下载的文件名是中文的话,需要对其进行字符编码转换,否则file_exists()函数不能识别,可以使用iconv()函数进行编码转换。
$file_sub_path() 我使用的是绝对路径,执行效率要比相对路径高。
Header("Accept-Ranges: bytes")的作用:告诉客户端浏览器返回的文件大小是按照字节进行计算的
Header("Accept-Length:".$file_size)的作用:告诉浏览器返回的文件大小
Header("Content-Disposition: attachment; filename=".$file_name)的作用:告诉浏览器返回的文件的名称
fclose($fp)可以把缓冲区内最后剩余的数据输出到磁盘文件中,并释放文件指针和有关的缓冲区。