PDA

View Full Version : Get remote image and store it in mysql BLOB


guvenck
01-18-2007, 04:25 PM
Hi,

I would like to know how I can:

1- Copy a remote image (known URL) to a directory on my server

2- preferrably store it in an BLOB field?

There will be LOTS of images (thousands) with 5-10KB each, so is it wise to store them in the db instead of the filesystem?


Regards,

guvenck
01-18-2007, 04:55 PM
And it seems that I need to use it with fopen(). My server has no CURL support.

firepages
01-18-2007, 11:58 PM
There are few good reasons to put images in a DB though there are some (archive?) situations where it might make sense, in general thats not what a DB is for.

<?
$file=file_get_contents($image_url);
//$file now has the image you could stick it in the DB//
$fp=fopen(basename($img_url),'wb');
fputs($fp,$file);
fclose($fp);
?>

guvenck
01-19-2007, 08:08 AM
Hi,

Thanks for your code, I will try it asap. Meanwhile, I've found this (and its working), what is the difference of using buffers?


$url = "http://imageurl";
$filename = "filenametosave";
ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);
$fp2 = @fopen($filename, "ab");
fwrite($fp2,$img);
fclose($fp2);

firepages
01-20-2007, 12:53 AM
what is the difference of using buffers?

typing practice ? ;)

guvenck
01-20-2007, 02:26 PM
Can you give an example if I'd do it with CURL? CURL is much faster and has many advantages...

firepages
01-21-2007, 01:32 AM
CURL is not faster.
It has advantages when you need to do more than a simple HTTP request, for a simple HTTP request with no other frills then fopen() or file_get_contents() is faster and far easier.

guvenck
01-21-2007, 03:13 PM
Read somewhere that there is a blocking problem on some servers for fopen().