//需求是这样,再不改变目录结构的前提下,通过URL,批量下载文件,我已经写好了一点儿了 //但是有点bug,只能下载最后一个文件 //邱老师解决一下 <Php? $file=fopen("q.txt","r"); $superfluous = strlen("http://www.greenpeace.org/china/Global/china/"); if($file) { $url_arr=array(); while (!feof($file)) { $url=fgets($file); array_push($url_arr,$url); } $url_arr_len=count($url_arr); for($i=0;$i<$url_arr_len;$i++){ $url_item=$url_arr[$i]; $file_name=basename($url_item); $name_length=strlen($file_name); $path_str=substr($url_item,$superfluous); $length=strlen($path_str)-$name_length; $path="./upload/".substr($path_str,0,$length); getFile($url_item,$path,$file_name); } fclose($file); } function getFile($url,$save_dir,$filename){ if(trim($url)==''){ return false; } if(trim($save_dir)==''){ $save_dir='./'; } //创建保存目录 if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){ return false; } //获取远程文件所采用的方法 $ch=curl_init(); $timeout=5; curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $content=curl_exec($ch); curl_close($ch); //文件大小 $fp1=fopen($save_dir.$filename,"a"); if(fq) fwrite($fp1,$content); fclose($fp1); return array('file_name'=>$filename,'save_path'=>$save_dir.$filename); } ?> //以下是q.txt里的内容 /* http://www.greenpeace.org/china/Global/china/_planet-2/2010/4/earth-day-2010.jpg http://www.greenpeace.org/china/Global/china/_planet-2/banners/2007/1/way-to-green.jpg http://www.greenpeace.org/china/Global/china/_planet-2/banners/2007/10/great-whale-trail-name-a-wha.gif http://www.greenpeace.org/china/Global/china/_planet-2/banners/2007/10/GWT_mainbanner.gif */
pardon110