Home > Backend Development > PHP Tutorial > php批量下载图片,网上觅的代码,不好使

php批量下载图片,网上觅的代码,不好使

WBOY
Release: 2016-06-13 12:16:28
Original
1251 people have browsed it

php批量下载图片,网上找的代码,不好使


<br />function project_statistics(){<br />    	echo "11111";<br />    	$url = "http://www.xxx.com:81/upload/image";  <br />		$content=file_get_contents($url);  <br />		$reg="/<img .*?src=\"(.*?)\".*? alt="php批量下载图片,网上觅的代码,不好使" >/";  <br />		  <br />		preg_match_all($reg,$content,$matches);  <br />		  <br />		$path = '../download/img';  <br />		if(!file_exists($path)){  <br />		    mkdir($path, 0777);  <br />		}  <br /><br />		function downImage($url,$filename="") {  <br />		    if($url=="") return false;  <br />		  <br />		    if($filename=="") {  <br />		        $ext=strrchr($url,".");  <br />		        if($ext!=".gif" && $ext!=".jpg" && $ext!=".png" && $ext!="jpeg") return false;  <br />		        $filename=date("YmdHis").$ext;  <br />		    }  <br />		  <br />		    ob_start();  <br />		    //make file that output from url goes to buffer  <br />		    readfile($url);  <br />		    //file_get_contents($url);  这个方法不行的!!!只能用readfile  <br />		    $img = ob_get_contents();  <br />		    ob_end_clean();  <br />		  <br />		    [email&#160;protected]($filename, "a");//append  <br />		    fwrite($fp,$img);  <br />		      <br />		    fclose($fp);  <br />		  <br />		    return $filename;  <br />		    	<br />		  }<br />		for($i = 0;$i < count($matches[1]);$i ++){  <br />		      <br />		    /*explode <br />		    $url_arr[$i] = explode('/', $matches[1][$i]); <br />		    $last = count($url_arr[$i])-1; <br />		    */  <br />		      <br />		    //strrchr     <br />		    $filename = strrchr($matches[1][$i], '/');  <br />		      <br />		    downImage($matches[1][$i],$path.$filename);  <br />		    //downImage($matches[1][$i],$path.'/'.$url_arr[$i][$last]);  <br />		}  <br />  <br /><br />}<br />
Copy after login

求教 ,谢谢。  就是想把上传到服务器上的图片,都下载本地。这个还需要改
------解决思路----------------------
你给出的代码是采集目标网页上所有的图片标签,并下载之。。

你如果想下载上传到服务器上的图片,那你可以尝试通过上述代码中的downImage函数,把服务器上图片的url地址传入下载之。
------解决思路----------------------
有圖片url嗎?
循環所有url,然後使用file_get_contents下載就可以了。

例如:
$url = "http://www.abc.com/1.jpg';
$c = file_get_contents($url);
file_put_contents(basename($url), $c, true);

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template