2013-03-15 94 views
4

我試圖從外部服務器下載大量文件(大約3700張圖像)。這些圖像每個從30KB到200KB。使用PHP(很多圖像)從遠程服務器下載多個圖像

當我在1張圖片上使用copy()函數時,它起作用。當我在一個循環中使用它時,我得到的只有30B圖像(空圖像文件)。

我試過使用copy,cURL,wgetfile_get_contents。每次我都會得到很多空文件,或者根本沒有。

這裏是我試過的代碼:

wget的:

exec('wget http://mediaserver.centris.ca/media.ashx?id=ADD4B9DD110633DDDB2C5A2D10&t=pi&f=I -O SIA/8605283.jpg'); 

副本:

if(copy($donnees['PhotoURL'], $filetocheck)) { 
    echo 'Photo '.$filetocheck.' updated<br/>'; 
} 

捲曲:

$ch = curl_init(); 
$source = $data[PhotoURL]; 
curl_setopt($ch, CURLOPT_URL, $source); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$data = curl_exec ($ch); 
curl_close ($ch); 

$destination = $newfile; 
$file = fopen($destination, "w+"); 
fputs($file, $data); 
fclose($file); 

一切似乎正常工作。不幸的是,我沒有太多的選擇一次下載所有這些文件,我需要一種方法使其儘快發揮作用。

非常感謝,安託萬

+0

他們可能會阻止像這樣的大規模下載。嘗試詢問服務器/服務的管理員。 – Sammitch 2013-03-15 15:40:44

+0

這可能是有道理的,但這臺服務器是這樣的大規模下載,這是一個系統,我已經去自己的圖像,而不是他們通過FTP發送給我。 – 2013-03-15 20:23:06

回答

5

我用這個函數,工作得很好。

function saveImage($urlImage, $title){ 

    $fullpath = '../destination/'.$title; 
    $ch = curl_init ($urlImage); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); 
    $rawdata=curl_exec($ch); 
    curl_close ($ch); 
    if(file_exists($fullpath)){ 
     unlink($fullpath); 
    } 
    $fp = fopen($fullpath,'x'); 
    $r = fwrite($fp, $rawdata); 

    setMemoryLimit($fullpath); 

    fclose($fp); 

    return $r; 
} 

與此另一種防止內存溢出組合:

function setMemoryLimit($filename){ 
    set_time_limit(50); 
    $maxMemoryUsage = 258; 
    $width = 0; 
    $height = 0; 
    $size = ini_get('memory_limit'); 

    list($width, $height) = getimagesize($filename); 
    $size = $size + floor(($width * $height * 4 * 1.5 + 1048576)/1048576); 

    if ($size > $maxMemoryUsage) $size = $maxMemoryUsage; 

    ini_set('memory_limit',$size.'M'); 

} 
+0

一開始沒有工作,但似乎現在工作。在變量「fullpath」中,我使用了「/ home/mls/public_html ...」,這顯然是錯誤的。非常感謝,我現在可以下載圖片了,謝謝! – 2013-03-15 21:19:09

+0

歡迎您:)祝你好運! – Alvaro 2013-03-18 10:40:35

9

讓他們一個一個可能相當緩慢。考慮將它們分成20-50個圖像的包並用多個線程抓取它們。下面的代碼讓你開始:

$chs = array(); 
$cmh = curl_multi_init(); 
for ($t = 0; $t < $tc; $t++) 
{ 
    $chs[$t] = curl_init(); 
    curl_setopt($chs[$t], CURLOPT_URL, $targets[$t]); 
    curl_setopt($chs[$t], CURLOPT_RETURNTRANSFER, 1); 
    curl_multi_add_handle($cmh, $chs[$t]);  
} 

$running=null; 
do { 
    curl_multi_exec($cmh, $running); 
} while ($running > 0); 

for ($t = 0; $t < $tc; $t++) 
{ 
    $path_to_file = 'your logic for file path'; 
    file_put_contents($path_to_file, curl_multi_getcontent($chs[$t])); 
    curl_multi_remove_handle($cmh, $chs[$t]); 
    curl_close($chs[$t]); 
} 
curl_multi_close($cmh); 

我用這種方法來獲取一些百萬計的圖像最近,因爲一個接一個將採取長達一個月。

一次抓取的圖像數量應該取決於它們的預期大小和內存限制。