- function make_dir($path){
- if(!file_exists($path)){//不存在则建立
- $mk=@mkdir($path,0777); //权限
- @chmod($path,0777);
- }
- return true;
- }
- function read_filetext($filepath){
- $filepath=trim($filepath);
- $htmlfp=@fopen($filepath,"r");
- //远程
- if(strstr($filepath,"://")){
- while($data=@fread($htmlfp,500000)){
- $string.=$data;
- }
- }
- //本地
- else{
- $string=@fread($htmlfp,@filesize($filepath));
- }
- @fclose($htmlfp);
- return $string;
- }
- function write_filetext($filepath,$string){
- //$string=stripSlashes($string);
- $fp=@fopen($filepath,"w");
- @fputs($fp,$string);
- @fclose($fp);
- }
- function get_filename($filepath){
- $fr=explode("/",$filepath);
- $count=count($fr)-1;
- return $fr[$count];
- }
- function save_pic($url,$savepath=''){
- //处理地址
- $url=trim($url);
- $url=str_replace(" ","%20",$url);
- //读文件
- $string=read_filetext($url);
- if(emptyempty($string)){
- echo '读取不了文件';exit;
- }
- //文件名
- $filename = get_filename($url);
- //存放目录
- make_dir($savepath); //建立存放目录
- //文件地址
- $filepath = $savepath.$filename;
- //写文件
- write_filetext($filepath,$string);
- return $filepath;
- }
- //目标图片地址
- $pic = "http://img0.pconline.com.cn/pconline/1205/06/2776119_end1_thumb.jpg";
- //保存目录
- $savepath = "images/";
- echo save_pic($pic,$savepath);
- function get_pic($cont,$path){
- $pattern_src = '/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/';
- $num = preg_match_all($pattern_src, $cont, $match_src);
- $pic_arr = $match_src[1]; //获得图片数组
- foreach ($pic_arr as $pic_item) { //循环取出每幅图的地址
- save_pic($pic_item,$path); //下载并保存图片
- echo "[OK]..!";
- }
- }
- $url = "http://gz.pconline.com.cn/321/3215791.html";
- $content = file_get_contents($url);//获取网页内容
- $preg = '#<div class="art_con">(.*)<div class="ivy620 ivy620Ex"></div>#iUs';
- preg_match_all($preg, $content, $arr);
- $cont = $arr[1][0];
- get_pic($cont,'img/');