2016-07-28 34 views
1

我試圖從數組中下載所有圖像,並將它們存儲在使用PHP的服務器上。PHP:從網址列表中下載圖片?

這是我的PHP代碼:

$IMAGES = 'http://url.com/image.jpg, http://url.com/image2.jpg, http://url.com/image-test.jpg, http://url.com/image6.jpg, http://url.com/image.jpg'; 

$images = array(''.$IMAGES.''); 

foreach($images as $name=>$image) { 

    $name = explode(",", $image); 

    $name0 = implode(" ",$name); 

    copy(''.$name0.'', '/test/'.$name0.'.jpg');  


} 

當我運行我的代碼,我沒有得到我的存儲服務器上的任何圖像和我在我的PHP頁面上收到一條警告消息。

請問有人能就這個問題提出建議嗎?

警告消息我得到的是這樣的:

Warning: copy(/test/http:/url.com/image.jpg http:/url.com/image2.jpg in line 88 

,這是行88:

copy(''.$name0.'', '/test/'.$name0.'.jpg'); 
+0

有什麼警告信息?爲什麼你會使用這樣的數組?您將圖像路徑保存爲數據庫中的單個字符串還是什麼? – Ionut

+0

傳遞給副本的URL看起來不對,將圖像定義爲數組:$ IMAGES = array('http://url.com/image.jpg','http://url.com/image2.jpg',' http://url.com/image-test.jpg','http://url.com/image6.jpg','http://url.com/image.jpg'); P.S不要混合那樣的變量,你可能會有一段糟糕的時間...... –

回答

1

嘗試以下操作:

$IMAGES = 'http://url.com/image.jpg, http://url.com/image2.jpg, http://url.com/image-test.jpg, http://url.com/image6.jpg, http://url.com/image.jpg'; 

$images = explode(', ',$IMAGES); 

foreach($images as $image) { 

    $name = basename($image); 
    $newfile = $_SERVER['DOCUMENT_ROOT'] .'/test/'.$name; 

    if(copy($image, $newfile)){ 
     echo 'Successfully downloaded '. $image; 
    }else{ 
     echo 'Download failed for '. $image; 
    } 

} 
0

你的陣列是含有1名名單爲它工作,你必須做這樣的事情

$images= array(
    'http://url.com/image.jpg', 
    'http://url.com/image2.jpg', 
    'http://url.com/image-test.jpg', 
    'http://url.com/image6.jpg', 
    'http://url.com/image.jpg' 
); 

不能按照自己想要的方式將字符串設置爲數組 - 但可以使用explode通過做這樣

$images = explode(',',$IMAGES); 

的東西,但是你得到一個問題與一個網址,「爆炸」的字符串到數組中,它