首页 > 代码库 > php下载图片生成原始路径 并将下载过的图片路径保存到txt文件中

php下载图片生成原始路径 并将下载过的图片路径保存到txt文件中

<?php//header("content-Type: text/html; charset=utf-8");@ini_set(‘date.timezone‘, ‘Asia/Shanghai‘);@set_time_limit(0);@ini_set(‘memory_limit‘,‘100M‘);$headurl = ‘http://‘.$_SERVER[‘HTTP_HOST‘].$_SERVER["REQUEST_URI"];$headurl = substr($headurl, 0, strrpos($headurl, ‘/‘) + 1);//--- 下载图片到本地_生成原文件夹路径 ---//$savetxt = date(‘Y-m-d H-i-s‘).‘.txt‘;//echo $savetxt.‘<br />‘;if(ob_get_level() == 0) ob_start();echo ‘<font style="color:#009900;">Downloading...</font><br /><br />‘;echo str_pad(‘‘,4096)."\n";ob_flush();flush();sleep(1);//$imgurls_array = file(‘imgurlstest.php‘); //读取文件中每行做为一个数组元素//$imgurls_array = array_unique(array_filter($imgurls_array));$file1 = ‘imglist.txt‘;$content1 = file_get_contents($file1);$imgurls_array = explode("\r\n", $content1);//读取文件中每行做为一个数组元素$imgurls_array = array_unique(array_filter($imgurls_array));if(!empty($imgurls_array)){ //--- 下载图片到本地_生成原文件夹路径 ---//    $success = array();    echo ‘<font style="color:#009900;">Have img Total: ‘ . count($imgurls_array) . ‘</font><br /><br />‘;    echo str_pad(‘‘,4096)."\n";    ob_flush();    flush();    sleep(1);    foreach($imgurls_array as $key => $imgurlval){        $imgurlval = trim(str_replace("\r\n", "", $imgurlval));               $temp = get_images($imgurlval);        if($temp){            $success[] = ‘<font style="color:#009900;">Success: <a style="color:#009900;" href="http://www.mamicode.com/‘.$headurl.$temp.‘">‘.$headurl.$temp.‘</a></font><br />‘;            $f=fopen($savetxt,"a");            fwrite($f, $imgurlval."\r\n");            fclose($f);        }           }       echo ‘<font style="color:#009900;">Success Total: ‘ . @count($success) . ‘</font><br /><br />‘;    echo str_pad(‘‘,4096)."\n";    ob_flush();    flush();    sleep(1);    $success = array_filter($success);    foreach($success as $key => $val){        echo $val;    }}function get_images($img_url){    if(empty($img_url)) return false;    $imgfile = pathinfo($img_url, PATHINFO_BASENAME);    $imgdir = pathinfo($img_url, PATHINFO_DIRNAME);    $imgdir = preg_replace("~http://\w*\.*([^/]*)/~i", "", $imgdir) . ‘/‘;    !is_dir($imgdir) && @mkdir($imgdir, 0777, true);    $ext =strrchr($img_url,".");    if($ext != ".gif" && $ext != ".jpg" && $ext != ".png" && $ext != ".bmp" && $ext != ".GIF" && $ext != ".JPG" && $ext != ".PNG" && $ext != ".BMP") return false;    $img_url = trim($img_url);    $img_url = str_replace(‘ ‘, ‘%20‘, $img_url);    $img_url = str_replace(‘&amp;‘, ‘&‘, $img_url);           $curl_handle = curl_init();           curl_setopt($curl_handle, CURLOPT_URL, $img_url);    curl_setopt($curl_handle, CURLOPT_HEADER, false);    curl_setopt($curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0");    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 30);    $img = curl_exec($curl_handle);    curl_close($curl_handle);           $handle = @fopen($imgdir.$imgfile, "w");       fwrite($handle, $img);    fclose($handle);    $down_img = $imgdir.$imgfile;    return $down_img;}ob_end_flush();?>

 

php下载图片生成原始路径 并将下载过的图片路径保存到txt文件中