2013-03-08 66 views
-1

我的供應商爲我提供了所有產品及其圖片的xml文件。動態下載圖片

的圖像在suplyer的DB主辦,因此在XML文件看起來像www.webservice.supplier.com/index.php?action=getimage & ID = 10

我怎樣才能下載此圖片或在我的opencart系統中使用?

回答

0

你可以嘗試打開與的file_get_contents,FOPEN或任何其他相關的PHP函數的文件。一旦打開到流中,您可以使用一組相關函數(fwrite等)將其保存。

上的細節,這是一些快速和骯髒的PHP代碼。

//This is the source image... 
$url='http://www.google.es/logos/2013/womens_day_2013-1055007-hp.jpg'; 

$file=fopen($url, 'r'); //Check with your server's provider, just in case they won't allow you to do this. 

if(!$file) 
{ 
    die('ERROR: Could not read file'); 
} 
else 
{ 
    //This is where you'll store the file. Check the directory permissions. 
    $destination=fopen('results/file_in_disk_.jpg', 'w'); 

    if(!$destination) die('ERROR: Could not create or open destination file'); 
    else 
    { 
     while($data=fread($file, 1024)) 
     { 
      fwrite($destination, $data); 
     } 

     fclose($destination); 
     fclose($file); 
    } 
} 

圖像提供商很可能會輸出與該網址相關聯的圖像標頭,所以不要擔心,如果源url看起來不像圖像。

請記住,你應該有你的供應商的下載和使用的圖像許可。

希望有所幫助。

編輯:我忘了,你應該換一些其他的一塊從XML文件讀取並就執行該代碼。不要忘記檢查已經存在的圖像!

+0

非常感謝!它的偉大工程,另外,如果我不得不做一些tansformation到文件...所以我去的網頁www.webservice.supplier.com/index.php?action=getimage&id=10和需要產生新的URL在圖像出現,之後將該網址複製並在代碼 – user2120473 2013-03-08 13:24:14

+0

最後一個問題貼:如果我想將圖像與URL中出現的編號重命名(www.webservice.supplier.com/index .php?action = getimage&id = 10)我該怎麼辦? – user2120473 2013-03-08 13:25:02

+0

對不起,我可以做,因爲有很多圖像嗎?當然,我有我的供應商的許可 – user2120473 2013-03-08 13:27:50