Jump to content


This topic is now archived and is closed to further replies.


Uploading images into mysql - tutorial anyone?

Recommended Posts

Does anyone know of a place that has a tutorial on uploading gif/jpg\'s into mysql? The tutorials here only tell you how to put it into a directory on your server.



Share this post

Link to post
Share on other sites

What you need to do, is follow the instructions for putting it in the directory, but once the file is uploaded in to it\'s temporary location, use the function fopen() to open the file, read the contents of the file in to a variable, use the functions chunk_split(base64_encode()); to encode the file properly, input the variable into MySQL. when you\'re ready to take it back out and display it, set the encoded data to a variable and use this the IMG SRC tag setting SRC=\"data:$filetype;base64,$encoded_data\"


$result=mysql_query("select data,filetype from images where id=1");




echo "<img src="data:$filetype;base64,$encoded_data">";


I\'ve noticed however that MSIE 6 has a problems with it, but NS7 works, if any one knows if IE can do the Inline images, please inform us.

The code for the first part is something like this...



if ($submit!="Submit") {

      echo "<table align=center>";

      echo "<center><form ENCTYPE="multipart/form-data" method=post action=album.php?mode=upload>";

      echo "<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="0">";

      echo "<TR><TD>File: <TD><input name="file[]" type=file>";         

      echo "<input type=submit name=submit value="Submit"></form>";

} else {





      $handle = fopen("$file",\'rb\');

      $file_content = fread($handle,$file_size);


      $encoded_data= chunk_split(base64_encode($file_content));

      mysql_query(insert into images set data=\'$encoded_data\',filetype=\'$file_type\',name=\'$file_name\',size=\'$file_size\') or die(mysql_error());

      header ("Location: index.php");




Sorry if I\'ve mad any mistakes in there, I\'m pulling it from the back of my mind. I hope it helps.



Share this post

Link to post
Share on other sites

Thanks for the info. But I\'m a newbie to this stuff and what you said was kinda complicated for me so I\'m going to just upload the images to a directory on my server then store the name of the file uploaded in a char cell in my db.


I actually have another question about database tables I\'m going to post now if you can help me with that I\'d love it. Its probably easier than what I just asked about.

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.