Jump to content

Uploading images into mysql - tutorial anyone?


barrow

Recommended Posts

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");

$data=mysql_fetch_row($result);

$encoded_data=$data[0];

$filetype=$data[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...


<?php

$submit=$_POST["submit"];

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 {

      $file=$_FILES["file"][\'tmp_name\'][0];

      $file_name=$_FILES["file"][\'name\'][0];

      $file_size=$_FILES["file"][\'size\'][0];

      $file_type=$_FILES["file"][\'type\'][0];

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

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

       fclose($handle);

      $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.

 

-Nevets

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.