Home >php教程 >PHP源码 >ecshop 远程商品详情图片本地化

ecshop 远程商品详情图片本地化

PHP中文网
PHP中文网Original
2016-05-25 17:04:12939browse

php代码

<?php
set_time_limit(0);
include("data/config.php");

$link=mysql_connect($db_host,$db_user,$db_pass)or die("connect error");
mysql_select_db($db_name,$link)or die(mysql_error());
mysql_query("set names utf8");

header("Content-type:text/html;charset=utf-8");

$id=intval($_GET[&#39;id&#39;]);


if($id==0){

$sql="select `goods_id`,`goods_name`,`goods_desc` from `p_goods` order by goods_id desc ";
$res=mysql_query($sql);
while($row=mysql_fetch_array($res))
{
$aa="<span style=&#39;color:#ccc;font-size:12px;&#39;>无远程图片</span>";
if(preg_match(&#39;/(http:\/\/)/i&#39;, $row[&#39;goods_desc&#39;])){
	$aa="<a target=&#39;_blank&#39; href=&#39;?id=".$row[&#39;goods_id&#39;]."&#39;>Go Get It</a>";
}
	echo $row[&#39;goods_id&#39;]." ".$row[&#39;goods_name&#39;]." ".$aa."<br/>";
}

exit();
}





sleep(1);


$sql="select `goods_desc` from `p_goods` where `goods_id`=&#39;$id&#39; limit 1";
$res=mysql_fetch_array(mysql_query($sql));

preg_match_all(&#39;/src=[\&#39;|"]([^"|^\&#39;]+)[\&#39;|"]/i&#39;, $res[0], $picurl); 
$urlArr=$picurl[1];
/*
print_r($urlArr);
die();
$urlArr[]="http://static.oschina.net/uploads/user/129/258733_50.jpg";
$urlArr[]="http://static.oschina.net/uploads/img/201302/16195715_kPci.png";
*/
$num= isset($_GET[&#39;num&#39;]) ? $_GET[&#39;num&#39;] : 1;

$all=count($urlArr);


$url=$urlArr[$num-1]; 


if(!preg_match(&#39;/(http:\/\/)/i&#39;, $url)){
	if($num >= $all) {
	exit(&#39;<br/>all done!&#39;);
	}
	echo $url."<br/> 已处理";
	echo "<script>location.href=&#39;?id={$id}&num=".($num+1)."&#39;</script>";
	exit();
} 


 
$array=@get_headers($url,1);

echo $url."<br/><br/>";

if(preg_match(&#39;/200/&#39;,$array[0])){

	ob_start();	
	readfile($url);
	$img = ob_get_contents();
	ob_end_clean(); 
 	$dir="images/goods_desc";
	
	  if (!file_exists($dir))
	  {
	   mkdir($dir, 0777);
	  }
	
	$filename=$dir."/".mt_rand(11111,9999999999).time().".jpg";
	$f=fopen($filename,&#39;a&#39;);
	$fres=fwrite($f,$img);
	fclose($f);
	
	if($fres!=false){
		$goods_desc=str_replace($url,"/".$filename,$res[0]);
		$sql="update `p_goods` set  `goods_desc` = &#39;$goods_desc&#39; where `goods_id`=&#39;$id&#39; limit 1 ";
		mysql_query($sql);
	}
	
}

echo $num."/".$all;

if($num >= $all) {

exit(&#39;<br/>all done!&#39;);
}
else{

echo "<script>location.href=&#39;?id={$id}&num=".($num+1)."&#39;</script>";
}
?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn