2012-07-14 34 views
0

我有一個簡單的php代碼,它加載一個外部XML文件並將圖片URL加載到我的數據庫中。從XML文件中加載整個圖片而不是URL內的數據庫

然後我把這個網址顯示在我的網站上。

問題是我最終從我的網站上的其他網站加載圖片,這會影響加載時間 - 現在每頁加載20張圖片。

所以我想,有沒有辦法將圖像完全存儲到我的數據庫,而不是隻是URL?

下面是代碼:

$myfeed = 'xmlfeed.xml'; 

    $myfeed_xml = simplexml_load_file($myfeed); 

    foreach($myfeed_xml->info as $myinfo){ 
     $pic0 = $myinfo->picture_url[0]; 
     $pic1 = $myinfo->picture_url[1]; 
     $pic2 = $myinfo->picture_url[2]; 
     $pic3 = $myinfo->picture_url[3]; 
     $pic4 = $myinfo->picture_url[4]; 

     if($pic0 != ''){ 

      mysql_query("INSERT INTO ".$table." (pic0, pic1, pic2, pic3, pic4) VALUES ('$pic0', '$pic1', '$pic2', '$pic3', '$pic4')", $dbh); 

     } 
    } 

謝謝!

+0

是的,這是可能的。您的PHP腳本需要獲取圖像數據並將其插入到MySQL blob中。或者,取出圖像但將其保存到磁盤。 – 2012-07-14 14:40:23

回答

0

爲什麼不將它們全部下載到您的服務器上,並使用服務器上的鏈接更新數據庫?只要版權policie(S)都還行吧......

// do your DB fetching here 

//loop through all current db links 
foreach($sqlResult as $result) 
{ 

// build up file path to store newly downloaded image 
$fPath="someFolder/pictures/"; 

// get/generate a name for the pics (I'll just use a radom number here, but you should avoid doing so if you are working with lots of urls as dups may happen) 
$iName=mt_rand(); 

//join path and url together 
$pAndURL=$fPath.$iName; 

// get and put data 
file_put_contents($pAndURL,file_get_contents($result['collumnWhereURLIsStored']); 

//now update your DB with the new link ($pAndURL) 

}//end of foreach 

那麼上面的代碼所做的僅僅是遍歷所有在你的數據庫的第三方鏈接和下載的內容(圖像)到您的服務器。然後,您可以簡單地使用自己的鏈接更新數據庫到特定圖像。簡單。但正如我之前提到的,首先檢查版權許可證,因爲我相信你現在不想陷入麻煩,嗯?

+0

Ty爲答案,我覺得Mysql的博客方法比較好,現在會檢查一下 – webmasters 2012-07-14 15:01:01

相關問題