php下载远程大文件方法(二)

header("Content-type: text/html; charset=UTF-8"); class download{ var $debug=true; var $errormsg=''; var $Filter=array(); var $filename=''; var $mineType='text/plain'; var $xlq_filetype=array(); function __construct($fileFilter='php,exe,html',$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,$fn) { $this->setfilename($filename); if($this->filecheck()) { //$fn = array_pop( explode( DIRECTORY_SEPARATOR, strtr( $this->filename, '',DIRECTORY_SEPARATOR ) ) ); try{ if(is_file($this->filename)) { $_filesize=filesize($this->filename); $_fp=fopen($this->filename,'rb'); } else { $urlinfo=parse_url($this->filename); $_filesize=0; $_host=isset($urlinfo['host'])? $urlinfo['host'] : $_SERVER['SERVER_NAME']; $_port=isset($urlinfo['port'])? $urlinfo['port'] : 80; $_path=isset($urlinfo['path'])? $urlinfo['path'] : ''; if($fn==''){ $fn=basename($_path); } $url = "tcp://{$_host}:{$_port}"; $connType='Close'; //1)拼装http请求头,下载文件到本地 $httpHeader= "GET {$_path} HTTP/1.1\r\n". "User-Agent: LongXing_Download_Module\r\n". "Host: {$_host}\r\n". "Connection: {$connType}\r\n\r\n"; //创建socket连接流。也可以使用fsockopen if (!$_fp = stream_socket_client($url)) { return false; } //请求文件流 fwrite($_fp,$httpHeader); //$meta = stream_get_meta_data($_fp); //获取文件大小 while($buffer!="\n" && $buffer!="\r\n") { //逐行读取响应文件流.读取一行并返回长度最多为 length - 1 字节的字符串。碰到换行符(包括在返回值中)、EOF 或者已经读取了 length - 1 字节后停止(看先碰到那一种情况)。如果没有指定 length,则默认为 1K,或者说 1024 字节 $buffer = fgets($_fp,2048); //获取文件大小 if(preg_match('/Content-Length:\s*(\d+)\s*/',$buffer,$match)==1){ $_filesize = intval($match[1]); } } } } catch(Exception $e) { $_file_size=0; if(!$_fp)return false; } //输出http请求头,弹窗下载 header( "Pragma: public" ); header( "Expires: 0" ); header( "Cache-Component: must-revalidate, post-check=0, pre-check=0" ); header( "Content-type:{$this->mineType}" ); if($_filesize>0){ header( "Content-Length: " .$_filesize ); } header( "Content-Disposition: attachment; filename={$fn} " ); header( 'Content-Transfer-Encoding: binary' ); while(!feof($_fp)){ //读取完最多 length 个字节数,或到达 EOF 的时候,或(对于网络流)当一个包可用时,或(在打开用户空间流之后)已读取了 8192 个字节时就会停止读取文件,视乎先碰到哪种情况 echo fread($_fp,8192); } fclose($_fp); return true; } else { return false; } } function geterrormsg() { return $this->errormsg; } function filecheck() { $filename=$this->filename; //if(file_exists($filename)) //{ $filetype=strtolower(substr($filename,strrpos($filename,'.')+1)); if(in_array($filetype,$this->Filter)) { $this->errormsg.=$filename.'不允许下载!'; if($this->debug) exit($filename.'不允许下载!') ; return false; }else { 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']="javascrīpt/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"; $this->xlq_filetype['mp3']="application/mp3"; $this->xlq_filetype['csv']="text/comma-separated-values"; } } try{ $todf = 'https://192.168.0.135:81/a.csv'; $df = 'th_a.csv'; $download=new download('php,exe,html',false); if(!$download->downloadfile($todf,$df)){ $errormsg = $download->geterrormsg(); } }catch(Exception $e){ exit($errormsg); }
评论

© toice | Powered by LOFTER