开发者

Storing affiliate image inside my database

开发者 https://www.devze.com 2023-03-06 09:08 出处:网络
This is a basic code which I use to pull info from my affiliate product feed. I\'m pulling, as you will see below, picture links...and store the urls into my databse. The problem is that I\'m showing

This is a basic code which I use to pull info from my affiliate product feed. I'm pulling, as you will see below, picture links...and store the urls into my databse. The problem is that I'm showing 20 products per page, and if the affiliate serves isn;'t working properly it slows donw my sites alot.

What i'd like to do is store the whole iamges somehow and hot the 开发者_如何学运维urls... I think that will improve my sites performance alot. Any ideeas?

$feed = 'my affiliate feed';

$xml = simplexml_load_file($feed);

foreach( $xml->productinfo as $productinfo )
{
    $pic0 = $productinfo->picture[0];
    $pic1 = $productinfo->picture[1];
    $pic2 = $productinfo->picture[2];

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

Thank you


First change pic0, pic1 and pic2 fields to BLOB type. (You might also want to store the MIME type with getimagesize() for use with header() when delivering the images.)

$feed = 'my affiliate feed';

$xml = simplexml_load_file($feed);

foreach( $xml->productinfo as $productinfo )
{
    for($i = 0; $i<=2; $i++) {
        $pic[$i] = mysql_real_escape_string(file_get_contents($productinfo->picture[$i]));
    }
    mysql_query("INSERT INTO $table (pic0, pic1, pic2) VALUES ('$pic[0]', '$pic[1]', '$pic[2]')");
}

I presume that you have an ID field in $table. Deliver images in a new PHP script like this:

if (!isset($_GET['id'])) die('No ID');
if (!isset($_GET['pic']) || !in_array($_GET['pic'], array(0, 1, 2)))
    $i='0';
else
    $i=mysql_real_escape_string($_GET['pic']);
$sql = sprintf( "SELECT pic$i FROM $table WHERE id=%s", mysql_real_escape_string($_GET['id']));
$result = mysql_query($sql) or die("Invalid query: " . mysql_error());
$row=mysql_fetch_array($result);
header("Content-type: image/jpeg");
echo $row[0];


well you can also store it as base64 blobs make the website not faster, the more db requests you have the more slow is the website if that are just a few images its ok and if you have a fast db server

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号