网站之家技术交流论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1391|回复: 0

ECSHOP远程图片本地化做法

[复制链接]
发表于 2012-11-1 01:41:48 | 显示全部楼层 |阅读模式
  1. <?php
  2. $dbadd = "localhost";
  3. $user = "root";
  4. $pw = "password";
  5. $db = "dbshujuku";
  6. $savefilepath = "E:/webroot/shop/remoteImages2";//本地存放路径
  7. $displayurl ="http://www.kinsumsun.com/remoteImages2";//外部显示路径
  8. $filter = "kinsumsun.com";//过滤(含有此网址的图片将过滤掉,不下载)
  9. ?>

  10. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  11. <html xmlns="http://www.w3.org/1999/xhtml">
  12. <head>
  13. <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
  14. <title>自动下载图片</title>
  15. </head>
  16. <script>

  17. </script>
  18. <body onload="document.all['from'].focus();">


  19. <form action="wj_downimg_auto.php" method="post">
  20. id: 从<input type="text" name="from" />到<input type="text" name="to" /><input type="submit" value="开始"/>
  21. </form>


  22. <?php

  23. if(empty($_POST['from']) || empty($_POST['to']) )
  24. {
  25. echo "请输入开始ID!";
  26. exit;
  27. }
  28. if(!is_numeric($_POST['from']) || !is_numeric($_POST['to']))
  29. {
  30. echo "输入有误!请重新输入";
  31. exit;
  32. }
  33. if($to && $to < $from)
  34. {
  35. echo "结束id不能小于开始id";
  36. exit;
  37. }
  38. $from = $_POST['from'];
  39. $to = $_POST['to'];
  40. function get_orifilename($filename)
  41. {
  42. $pos = strripos($filename, "/");
  43. $pos2 = strripos($filename, ".");
  44. return substr($filename, $pos + 1 );//, $pos2 - $pos - 1);
  45. }
  46. function zhoz_get_contents($url, $second = 5)
  47. {
  48. $ch = curl_init();
  49. curl_setopt($ch,CURLOPT_URL,$url);
  50. curl_setopt($ch,CURLOPT_HEADER,0);
  51. curl_setopt($ch,CURLOPT_TIMEOUT,$second);
  52. curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);

  53. $content = curl_exec($ch);
  54. curl_close($ch);
  55. return $content;
  56. }
  57. function downimg($id, $desc, $savefilepath, $filter, $db, $con, $displayurl)
  58. {
  59. $pattern="/http:\/\/(\w+\.)+(net|com|cn|org|cc|tv|hk)(\S*\/)(\S)+\.(gif|jpg|png|bmp|jpeg|GIF|JPG|PNG|BMP|JPEG)/i";
  60. preg_match_all($pattern, $desc,$matches);
  61. if(!$matches[0])
  62. {
  63. return;
  64. }
  65. $ljflag = 0;
  66. $total = 0;
  67. $flag = 0;
  68. for($i=0;$i<count($matches[0]);$i++)
  69. {
  70. for($ljtemp=0;$ljtemp<$i;$ljtemp++)
  71. {
  72. if($matches[0][$i]==$matches[0][$ljtemp])
  73. {
  74. $ljflag=1;
  75. break;
  76. }
  77. else
  78. {
  79. $ljflag=0;
  80. }
  81. }
  82. if(!$ljflag)
  83. if(!strpos($matches[0][$i],$filter))
  84. {
  85. //下载,保存
  86. $cnt = 0;
  87. echo "
  88. 正在下载<font color="red">(".($total+1).") </font>".trim($matches[0][$i]);
  89. $img = "";
  90. while(empty($img) && $cnt < 5)//最多尝试下载5次
  91. {
  92. $img = zhoz_get_contents(trim($matches[0][$i]));
  93. if($cnt > 0)
  94. {
  95. echo "
  96. <font color="red">第" . $cnt . "次下载失败,正在进行第" . ($cnt + 1) . "次下载...</font>";
  97. }
  98. $cnt++;
  99. }
  100. if(empty($img))
  101. {
  102. echo "
  103. <font color="red">下载失败!</font>";
  104. continue;
  105. }
  106. $flag = 1;
  107. $total++;
  108. $filenametemp = "/".$id."_";
  109. $filename = $filenametemp.get_orifilename($matches[0][$i]);
  110. $fFileSaveName=$savefilepath.$filename;
  111. while(file_exists($fFileSaveName))//遇到重复的文件名了...
  112. {
  113. $filenametemp = $filenametemp ."t_";
  114. $filename = $filenametemp.get_orifilename($matches[0][$i]);
  115. $fFileSaveName=$savefilepath.$filename;
  116. }
  117. $fp2=fopen($fFileSaveName, "a");
  118. fwrite($fp2,$img);
  119. fclose($fp2);
  120. echo "
  121. 正在保存    ".$displayurl.$filename;
  122. //替换
  123. $desc = str_ireplace($matches[0][$i], $displayurl.$filename, $desc);
  124. }
  125. }
  126. //更新数据库

  127. if($flag == 1)
  128. {
  129. echo "
  130. 正在更新数据库....";
  131. $desc = str_ireplace("'", "\'", $desc);
  132. $strsql = "update ecs_goods set goods_desc='".$desc."' where goods_id='".$id."'";//注意前缀
  133. mysql_db_query($db, $strsql, $con);
  134. if(mysql_error())
  135. {
  136. echo "
  137. <font color="red">更新出错!</font>";
  138. echo "
  139. ".mysql_error();
  140. }
  141. else
  142. {
  143. echo "
  144. 更新数据库成功!";
  145. }
  146. }
  147. return $total;
  148. }
  149. echo "
  150. 商品id从".$from."到".$to;
  151. $con = mysql_connect($dbadd,$user,$pw);
  152. mysql_query("SET NAMES 'GBK'");
  153. if (!$con)
  154. {
  155. die('Could not connect: ' . mysql_error());
  156. }
  157. $strsql="select goods_id, goods_desc from ecs_goods where goods_id >= " . $from . " and goods_id <= " . $to . " ";

  158. // 执行sql查询
  159. $result=mysql_db_query($db, $strsql, $con);
  160. $inum= 0;
  161. $totalpic = 0;
  162. set_time_limit(0);
  163. while ($row=mysql_fetch_row($result))
  164. {
  165. echo "
  166. 当前进行id为".$row[0]."的操作,已操作了".$inum."条数据......";
  167. $totalpic = $totalpic + downimg($row[0], $row[1], $savefilepath, $filter, $db, $con, $displayurl);
  168. $inum++;
  169. }

  170. echo "

  171. 操作完毕!共下载".$totalpic."张图片";
  172. mysql_close($con);





  173. ?>
  174. </body>
  175. </html>
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|网站之家技术交流论坛 ( 粤ICP备09092995号 )

GMT+8, 2024-5-20 02:09 , Processed in 0.105595 second(s), 10 queries , File On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表