从XML文件加载整个图片而不是URL在数据库中

时间:2012-07-14 14:35:49

标签: php mysql

我有一个简单的PHP代码,可以加载外部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);

        }
    }  

谢谢!

1 个答案:

答案 0 :(得分:0)

为什么不在服务器上全部下载它们,并使用服务器上的链接更新数据库?只要版权政策可以用它......

// 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

那么上面的代码只是通过数据库中的所有第三方链接并将其内容(图像)下载到您的服务器。然后,您只需使用自己的特定图像链接更新数据库。简单。但正如我之前提到的,首先检查版权许可,因为我确定你现在不想遇到麻烦,嗯?