利用php来采集图片方法很简单本实例使用了fopen来保存文件了,但在此小编建义大家不要使用此方法来获取,如果数据量大量fopen函数性能比curl差得远了,所以大家可尝试改进使用curl来处理,代码如下:
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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){
-
- $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;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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]..!<br />”;
- }
- }
-
-
-
-
- $url = ”你的网地址”;
-
- $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/’);
-
-
-
-
- ?>
|