1、打开cmd命令 进入到安装php的根目录,使用php -f 命令执行index.php文件所在的相对路经
2、编辑index.php代码
<?php
$url = ''; //url地址
$str=file_get_contents($url);
//取得第一个 img 标签,并储存至二维数组 $match 中
preg_match_all('/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i', $str, $matchs);
//打印出match
// print_r($matchs[0]);
$arr = [];
foreach ($matchs[0] as $v) {
$preg = '/http:\/\/.*.jpg/';
preg_match_all($preg, $v, $match);
if(!empty($match[0][0])){
$arr[] = $match[0][0];
}
}
//文件保存地址
$dir = 'E:/images/';
//print_r($arr);
foreach ($arr as $k => $v) {
$key = strrchr($v, "/"); //返回最后出现的后面值
$res = substr($key,1); //从第一位开始截取到最后,默认0开始
// 图片名称
$name = $dir . $res;
// 下载
download($name, $v);
}
function download($name, $url)
{
if (!is_dir(dirname($name))) { mkdir(dirname($name));
}
$str = file_get_contents($url);
$res = file_put_contents($name, $str);
if ($res) {
echo $name . "下载成功\n"; //\n必须使用双引号
} else {
echo $name . '失败';
}}
复制代码