|
- <?php
- $dbadd = "localhost";
- $user = "root";
- $pw = "password";
- $db = "dbshujuku";
- $savefilepath = "E:/webroot/shop/remoteImages2";//本地存放路径
- $displayurl ="http://www.kinsumsun.com/remoteImages2";//外部显示路径
- $filter = "kinsumsun.com";//过滤(含有此网址的图片将过滤掉,不下载)
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
- <title>自动下载图片</title>
- </head>
- <script>
- </script>
- <body onload="document.all['from'].focus();">
- <form action="wj_downimg_auto.php" method="post">
- id: 从<input type="text" name="from" />到<input type="text" name="to" /><input type="submit" value="开始"/>
- </form>
- <?php
- if(empty($_POST['from']) || empty($_POST['to']) )
- {
- echo "请输入开始ID!";
- exit;
- }
- if(!is_numeric($_POST['from']) || !is_numeric($_POST['to']))
- {
- echo "输入有误!请重新输入";
- exit;
- }
- if($to && $to < $from)
- {
- echo "结束id不能小于开始id";
- exit;
- }
- $from = $_POST['from'];
- $to = $_POST['to'];
- function get_orifilename($filename)
- {
- $pos = strripos($filename, "/");
- $pos2 = strripos($filename, ".");
- return substr($filename, $pos + 1 );//, $pos2 - $pos - 1);
- }
- function zhoz_get_contents($url, $second = 5)
- {
- $ch = curl_init();
- curl_setopt($ch,CURLOPT_URL,$url);
- curl_setopt($ch,CURLOPT_HEADER,0);
- curl_setopt($ch,CURLOPT_TIMEOUT,$second);
- curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
- $content = curl_exec($ch);
- curl_close($ch);
- return $content;
- }
- function downimg($id, $desc, $savefilepath, $filter, $db, $con, $displayurl)
- {
- $pattern="/http:\/\/(\w+\.)+(net|com|cn|org|cc|tv|hk)(\S*\/)(\S)+\.(gif|jpg|png|bmp|jpeg|GIF|JPG|PNG|BMP|JPEG)/i";
- preg_match_all($pattern, $desc,$matches);
- if(!$matches[0])
- {
- return;
- }
- $ljflag = 0;
- $total = 0;
- $flag = 0;
- for($i=0;$i<count($matches[0]);$i++)
- {
- for($ljtemp=0;$ljtemp<$i;$ljtemp++)
- {
- if($matches[0][$i]==$matches[0][$ljtemp])
- {
- $ljflag=1;
- break;
- }
- else
- {
- $ljflag=0;
- }
- }
- if(!$ljflag)
- if(!strpos($matches[0][$i],$filter))
- {
- //下载,保存
- $cnt = 0;
- echo "
- 正在下载<font color="red">(".($total+1).") </font>".trim($matches[0][$i]);
- $img = "";
- while(empty($img) && $cnt < 5)//最多尝试下载5次
- {
- $img = zhoz_get_contents(trim($matches[0][$i]));
- if($cnt > 0)
- {
- echo "
- <font color="red">第" . $cnt . "次下载失败,正在进行第" . ($cnt + 1) . "次下载...</font>";
- }
- $cnt++;
- }
- if(empty($img))
- {
- echo "
- <font color="red">下载失败!</font>";
- continue;
- }
- $flag = 1;
- $total++;
- $filenametemp = "/".$id."_";
- $filename = $filenametemp.get_orifilename($matches[0][$i]);
- $fFileSaveName=$savefilepath.$filename;
- while(file_exists($fFileSaveName))//遇到重复的文件名了...
- {
- $filenametemp = $filenametemp ."t_";
- $filename = $filenametemp.get_orifilename($matches[0][$i]);
- $fFileSaveName=$savefilepath.$filename;
- }
- $fp2=fopen($fFileSaveName, "a");
- fwrite($fp2,$img);
- fclose($fp2);
- echo "
- 正在保存 ".$displayurl.$filename;
- //替换
- $desc = str_ireplace($matches[0][$i], $displayurl.$filename, $desc);
- }
- }
- //更新数据库
- if($flag == 1)
- {
- echo "
- 正在更新数据库....";
- $desc = str_ireplace("'", "\'", $desc);
- $strsql = "update ecs_goods set goods_desc='".$desc."' where goods_id='".$id."'";//注意前缀
- mysql_db_query($db, $strsql, $con);
- if(mysql_error())
- {
- echo "
- <font color="red">更新出错!</font>";
- echo "
- ".mysql_error();
- }
- else
- {
- echo "
- 更新数据库成功!";
- }
- }
- return $total;
- }
- echo "
- 商品id从".$from."到".$to;
- $con = mysql_connect($dbadd,$user,$pw);
- mysql_query("SET NAMES 'GBK'");
- if (!$con)
- {
- die('Could not connect: ' . mysql_error());
- }
- $strsql="select goods_id, goods_desc from ecs_goods where goods_id >= " . $from . " and goods_id <= " . $to . " ";
- // 执行sql查询
- $result=mysql_db_query($db, $strsql, $con);
- $inum= 0;
- $totalpic = 0;
- set_time_limit(0);
- while ($row=mysql_fetch_row($result))
- {
- echo "
- 当前进行id为".$row[0]."的操作,已操作了".$inum."条数据......";
- $totalpic = $totalpic + downimg($row[0], $row[1], $savefilepath, $filter, $db, $con, $displayurl);
- $inum++;
- }
- echo "
- 操作完毕!共下载".$totalpic."张图片";
- mysql_close($con);
- ?>
- </body>
- </html>
复制代码 |
|