Home >Backend Development >PHP Tutorial >批量下载的图片都是0字节,该怎么解决

批量下载的图片都是0字节,该怎么解决

WBOY
WBOYOriginal
2016-06-13 10:24:041654browse

批量下载的图片都是0字节

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php $conn = mysql_connect("127.0.0.1","root","123456") or die("无法连接数据库");@mysql_query("SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary",$conn);mysql_select_db("s517",$conn) or die("无法连接数据库");global $db;function getImage($url, $filename="") {if(!$url) return false;if(!$filename) {  $ext=strrchr(strtolower($url),".") ;  if($ext!=".gif" && $ext!=".jpg" && $ext!=".png") return false;  $str=explode('/',$url) ;  $filename=$str[count($str)-1] ;}ob_start();readfile($url);$img = ob_get_contents();ob_end_clean();[email&#160;protected]($filename, "a") ;fclose($fp2) ;return $filename;}//批量下载    $result = mysql_query("SELECT itemid, thumb FROM table LIMIT 0 , 300");    while($row = mysql_fetch_array($result)) {    getImage($row["thumb"],"");    echo $row["itemid"]."输出成功";     }?>

table里面的thumb是采集的远程图片路径
我用这个函数下载下来的图片都是0字节。

------解决方案--------------------
while太快了,sleep一下
------解决方案--------------------
先去一两张看看有什么,而且你的代码里没有输出 getImage()的结果啊.
PHP code
    $result = mysql_query("SELECT itemid, thumb FROM table LIMIT 0 , 1");    while($row = mysql_fetch_array($result)) {    echo getImage($row["thumb"],"")."<br>";    echo $row["itemid"]."输出成功<br>";     }<br><font color="#e78608">------解决方案--------------------</font><br>直接file_get_content file_put_content<div class="clear">
                 
              
              
        
            </div>
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