<?php
/**
* 匹配下载网络资源
*/
header("Content-type: text/html;charset=utf-8");
error_reporting(E_ALL ^E_NOTICE^E_WARNING);
class DownloadFileFromWebsite{
private $img_ext_arr=array('WEBP','BMP','JPG','GIF','JPEG','PSD','EPS','PNG','RAW','EMF','ICO');
public $file_dir;
public $matches_x;
function __construct($web_url,$root_dir){
if(empty($web_url) || empty($root_dir)){
exit('参数错误!');
}
$this->file_dir=$this->creatDirByWebUrl($web_url,$root_dir);
$this->matches_x=$this->pregMatchUrl($web_url);
}
//通过地址获取文件内容
protected function getContentByUrl($url,$param_arr)
{
$param_sring=http_build_query($param_arr);
return file_get_contents($url.$param_sring);
}
//下载文件
protected function dowmload_file($file_url, $save_to,$logDir,$encodeName)
{
$file_name=basename($file_url);
$content = file_get_contents($file_url);
$result=file_put_contents($save_to, $content);
if($result){
$status='下载成功!';
$state='';
}else{
unlink($this->file_dir.'/img/'.$file_name);//删除空的错误文件
$status='下载失败!!!';
$state='[✘]';
}
//根据系统进行配置
$encode = stristr(PHP_OS, 'WIN') ? 'GBK' : 'UTF-8';
$arr_name_ext=explode('.',basename($save_to));
$filename=iconv($encode,'UTF-8',$encodeName);
$ext=$arr_name_ext[1];
$log_record=$state.'图片文件:'.$file_name.' ------ ['.$filename.'.'.$ext.'] ------ '.date('Y-m-d H:i:s',time()).' ------ 大小:'.round($result/1024,2).'kb ------'.$status.PHP_EOL;
echo $log_record.'<br>';
file_put_contents($logDir,$log_record,FILE_APPEND);
}
//根据访问地址建立文件夹目录
private function creatDirByWebUrl($url,$root_dir){
preg_match_all('/([http|https]*):\/\/*(.*?\/.*)/',$url,$match_web_url);
$dir_path=str_replace('/','_',$match_web_url[2][0]);
$this->file_dir=$root_dir.'/'.$match_web_url[1][0]."_".$dir_path;
if(!is_dir($this->file_dir.'/img')){
$staue=mkdir($this->file_dir.'/img',0777,true);
if(!$staue){
echo $dir_path.'目录创建失败!<br/>';
return false;
}
}
return $this->file_dir;
}
//根据设定规则,匹配要下载的资源
public function pregMatchUrl($url){
$param_arr=array();
$string_html=$this->getContentByUrl($url,$param_arr);
preg_match_all('/<img src=["|\']([http|https].*)["|\'] alt=["|\'](.*)["|\'].*\/*?>/U',$string_html,$matches_x);
return $matches_x;
}
//处理文件后缀,并下载资源
public function renameDownloadFiles(){
foreach($this->matches_x[1] as $k=>$v){
$arr=explode('.',basename($v));
$ext=strtoupper(end($arr));
//根据系统进行配置
$encode = stristr(PHP_OS, 'WIN') ? 'GBK' : 'UTF-8';
$this->matches_x[2][$k] = iconv('UTF-8', $encode, $this->matches_x[2][$k]);
if(in_array($ext,$this->img_ext_arr)){
$this->dowmload_file($v,$this->file_dir.'/img/'.$this->matches_x[2][$k].'.'.strtolower($ext),$this->file_dir.'/log.txt',$this->matches_x[2][$k]);
}else{
//处理特殊后缀,排除网站后缀干扰 如 jpg!| jpg@90
$str_ext='['.join('|',$this->img_ext_arr).']{1,}';
preg_match_all('/'.$str_ext.'/',strtoupper($ext),$match_all);
if($match_all){
$ext_new=$match_all[0][0];
}
$this->dowmload_file($v,$this->file_dir.'/img/'.$this->matches_x[2][$k].'.'.strtolower($ext_new),$this->file_dir.'/log.txt',$this->matches_x[2][$k]);
}
}
}
}
//$web_url='http://www.duok******.com/';
$pageCount=20;
for($a=1;$a<=$pageCount;$a++){
$web_url='http://www.duok*****.com/list/1-'.$a;
$root_dir='./DuoKan_DownloadFile';
$obj=new DownloadFileFromWebsite($web_url,$root_dir);
$obj->renameDownloadFiles();
}
PHP 正则匹配下载网络资源
最新推荐文章于 2021-03-10 17:46:28 发布