2011-05-14 126 views
0

我有一個產品形象從URL將圖像複製到服務器,刪除所有圖像後

http://www.myaffiliate.com/products/product.jpg 

我要複製的想象到我的網站,在一個文件夾,名爲讓我們說產品的推廣鏈接。它託管在共享的sherver上,而不是我的電腦上。我認爲PHP副本()不起作用,試用了它,我認爲我的主機不支持該方法。我怎麼能這樣做,捲曲也許?然後,我需要一個php命令來刪除產品文件夾中的所有圖像 - 我將使用cronjob完成此操作。在你希望你的服務器上的文件

<?php 
$table = 'cron'; 

$feed = 'myfeed'; 

$xml = simplexml_load_file($feed); 

mysql_query("TRUNCATE TABLE ".$table."", $dbh1); 
mysql_query("TRUNCATE TABLE ".$table."", $dbh2); 


function getimg($url) {   
     $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';    
     $headers[] = 'Connection: Keep-Alive';   
     $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';   
     $user_agent = 'php';   
     $process = curl_init($url);   
     curl_setopt($process, CURLOPT_HTTPHEADER, $headers);   
     curl_setopt($process, CURLOPT_HEADER, 0);   
     curl_setopt($process, CURLOPT_USERAGENT, $useragent);   
     curl_setopt($process, CURLOPT_TIMEOUT, 30);   
     curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);   
     curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);   
     $return = curl_exec($process);   
     curl_close($process);   
     return $return;  
     } 

foreach($xml->performerinfo as $performerinfo) 
{ 
    $performerid = $performerinfo->performerid; 
    $category = $performerinfo->category; 
    $subcategory = $performerinfo->subcategory; 
    $bio = $performerinfo->bio; 
    $turnons = $performerinfo->turnons; 
    $willing = $performerinfo->willingness; 
    $willingness = str_replace(',', ', ', $willing); 
    $age = $performerinfo->age; 
    $build = $performerinfo->build; 
    $height = $performerinfo->height; 
    $weight = $performerinfo->weight; 
    $breastsize = $performerinfo->breastsize; 
    $haircolor = $performerinfo->haircolor; 
    $hairlength = $performerinfo->hairlength; 
    $eyecolor = $performerinfo->eyecolor; 
    $ethnicity = $performerinfo->ethnicity; 
    $sexpref = $performerinfo->sexpref; 
    $pic0 = $performerinfo->picture[0]; 
    $pic1 = $performerinfo->picture[1]; 
    $pic2 = $performerinfo->picture[2]; 
    $pic3 = $performerinfo->picture[3]; 
    $pic4 = $performerinfo->picture[4]; 
    $test = $performerinfo->picture[0]; 

    $imgurl = 'http://www.foodtest.ru/images/big_img/sausage_3.jpg'; 
    $imagename= basename($imgurl); 
    if(file_exists('./tmp/'.$imagename)){continue;} 
    $image = getimg($imgurl); 
    file_put_contents('tmp/'.$imagename,$image); 

} 

    //baracuda reloaded 
    mysql_query("TRUNCATE TABLE reloaded", $dbh1); 
    mysql_query("TRUNCATE TABLE reloaded", $dbh2); 

    mysql_query("INSERT INTO reloaded SELECT * FROM ".$table."", $dbh1); 
    mysql_query("INSERT INTO reloaded SELECT * FROM ".$table."", $dbh2); 
?> 
+0

想要在下載圖像後刪除圖像正確嗎?如果是這樣,請調用unlink($ filename);在每個圖像上。與下面的Lawrence Cherone的腳本一起使用。 – 2011-05-14 14:07:56

+0

您使用相同的圖像網址,因此每次迭代都會調用相同的圖像,同樣,如果Feed中有很多或條目,那麼無論如何,在sql查詢中使用更多的邏輯將會加快速度,如果您檢查是否條目已添加等。 – 2011-05-14 14:47:08

回答

1

使用的file_get_contents或捲曲在一個循環

<?php 

    //loop with a list of images 
    loop{ 
    $imgurl = 'url to image'; 
    $imagename= basename($imgurl); 
    if(file_exists('./images_folder/'.$imagename)){continue;} 
    $image = file_get_contents($imgurl); 
    file_put_contents('images_folder/'.$imagename,$image); 
    } 
    ?> 

    or cURL 

    <?php 
    //loop this 
loop{ 
    $imgurl = 'url to image'; 
    $imagename= basename($imgurl); 
    if(file_exists('./images_folder/'.$imagename)){continue;} 
    $image = getimg($imgurl); 
    file_put_contents('images_folder/'.$imagename,$image); 
} 
    ?> 




    <?php 
    //cURL function (outside of loop) to get img 
     function getimg($url) { 
      $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg'; 
      $headers[] = 'Connection: Keep-Alive'; 
      $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8'; 
      $user_agent = 'php'; 
      $process = curl_init($url); 
      curl_setopt($process, CURLOPT_HTTPHEADER, $headers); 
      curl_setopt($process, CURLOPT_HEADER, 0); 
      curl_setopt($process, CURLOPT_USERAGENT, $useragent); 
      curl_setopt($process, CURLOPT_TIMEOUT, 30); 
      curl_setopt($process, CURLOPT_RETURNTRANSFER, 1); 
      curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1); 
      $return = curl_exec($process); 
      curl_close($process); 
      return $return; 
     } 
    ?> 

刪把這個它,把它在你的循環結束,一旦你得到了所有的圖像

<?php 
function rrmdir($dir) { 
    if (is_dir($dir)) { 
     $objects = scandir($dir); 
     foreach ($objects as $object) { 
      if ($object != "." && $object != "..") { 
       if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); 
      } 
     } 
     reset($objects); 
     rmdir($dir); 
     mkdir($dir); 
    } 
} 

rrmrir('./products'); 
?> 
+0

不要將getimg()函數放入循環中 – 2011-05-14 14:22:11

+0

致命錯誤:無法在/ web/sites/129中重新聲明getimg()(之前在/web/sites/129/website.com/cron/cron.php:49中聲明) /website.com/cron/cron.php 49行 – webmasters 2011-05-14 14:22:14

+0

Ty,現在勞倫斯我和我用過的其他方法有同樣的問題,頁面正在加載並且一直加載 – webmasters 2011-05-14 14:24:14