Jump to content


Uploading images into mysql - tutorial anyone?

  • Please log in to reply
2 replies to this topic

#1 barrow

  • Members
  • PipPip
  • Member
  • 15 posts

Posted 14 June 2003 - 09:21 AM

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.


#2 Nevets

  • Members
  • PipPip
  • Member
  • 10 posts

Posted 15 June 2003 - 12:38 AM

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.


#3 barrow

  • Members
  • PipPip
  • Member
  • 15 posts

Posted 15 June 2003 - 02:43 AM

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.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users