Forum Moderators: coopster

Message Too Old, No Replies

Need Help Savings Image to MySQL

Saving image to table instead of a directory

         

Gian04

7:53 am on Jun 18, 2007 (gmt 0)

10+ Year Member



Anyone who can post even just a simple script to save (INSERT) an image (JPG, GIF, etc) to a table instead of uploading it to a directory, and what data type should I use. Thanks

Habtom

8:19 am on Jun 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>> what data type should I use.
longtext

I think you just need to open $file_open=fopen($file, "r") and read it fread($file_open, $filesize);
//$filesize you need to find out the file size

chunk_split and base64_encode the file and insert it.

Habtom

vincevincevince

8:20 am on Jun 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The key is to get the image in a format you can then use to output to the browser, normally the raw jpeg (or similar format) file is fine.

If someone has uploaded a file, you will find it here:

$_FILES[fieldname][tmp_name]
where fieldname is the name="" parameter of your
<INPUT TYPE="FILE" 
element in your form.

You then read that file into memory:

$filecontents=file_get_contents($_FILES[fieldname][tmp_name]);

Insert it into MySQL by escaping it and putting it into a BLOB field:

mysql_query("INSERT INTO `images` (`imagename`,`imagecontents`) VALUES ('".mysql_escape_string($_FILES[fieldname][name])."','".mysql_escape_string($filecontents)."')");

When you eventually want to display it again, you'll need to get it from the database:

$r=mysql_fetch_assoc(mysql_query("SELECT * FROM `images` WHERE `imagename` = '$theimagename'"));

Then output an appropriate header:
header("Content-type: image/jpeg");

Then send out a file size:
header("Content-length: ".len($r[imagecontents]));

Finally, the image:
print $r[imagecontents];

If you want to dynamically do things with the image using GD... then create the GD image and store it in GD's own format - much faster for loading and quickly thumbnailing, cropping, whatever on-the-fly

Habtom

8:26 am on Jun 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yea, BLOB could do much better. I agree with that part.

Hab

[edited by: Habtom at 8:26 am (utc) on June 18, 2007]

vincevincevince

8:38 am on Jun 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I want to add that if the images are large, it's not generally advised to keep them in the database, in the interests of keeping the database of reasonable size. My quick-and-dirty solution is to use mysql_insert_id()...

First insert the record into a table which uses auto_increment on one field...

then:

file_put_contents("files/".mysql_insert_id(),file_get_contents($_FILES[fieldname][tmp_name]));

When it comes to recalling it... if the auto_increment field is 'imageid' then just:

$image=file_get_contents("files/".$r[imageid]);

And proceed as before.

Gian04

3:07 pm on Jun 18, 2007 (gmt 0)

10+ Year Member



Thanks vincevincevince

By the way is it possible to control the size before displaying it to browser?

[edited by: Gian04 at 3:23 pm (utc) on June 18, 2007]

joelgreen

5:35 pm on Jun 18, 2007 (gmt 0)

10+ Year Member



You will have to resize image on your server before displaying it. Try searching for "thumbnail generator in php" or something like this.
Ideally you would have big image and thumb stored on the server for each image. In this case no need to resize image each time - less server load.

I would also suggest to store images outside the database. Then you'll be able to link them directly from web page instead of loading from db each time. Will work faster.

vincevincevince

1:01 am on Jun 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



GD is the best for resizing the image - look up imagecopyresampled() at php.net