2012-07-14 57 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

相关问题