Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


How can I enter images into a MySQL database correctly through my MyPhPadmin?

Posted on 2004-11-02
Medium Priority
Last Modified: 2013-12-12
I have set up a dynamic database using Macromedia Dreamweaver, MySQL and MYPHPadmin and have managed to connect to my database through Dreamweaver, and am able to see the data when I preview my webpage. All's well there.

I now want to add images into my MySQL Database. I have tried to do this by entering another column in my table (through MYpHpadmin) and setting it to type 'LONGBLOB'. Attributes were then set to BINARY - (automatically, I think).

I then edited the field as per usual, and browsed (still in MyPHPAdmin) to my image on my local computer. (My MySQL and MyPhpAdmin are stored on a remote server).I seemingly selected an image and pressed 'GO'. The image seemed to go into the database as the filesize was then dispalyed in the appropriate column for the appropriate field in the table (although I could see no other evidence that it was actually in there).

Through Dreamwaver, I then linked a placed image to this column on the database (selecting it through a set-up recordset) but when I tried to view this in my browser, instead of my nice little image, I saw lines and lines of nonsense (non-decipherable letters and symbols).

Have I entered the images into the Database correctly? Any help / opinions on the matter would be greatly appreciated.



Question by:Adam
  • 3
  • 2

Expert Comment

ID: 12480554

There are actually three popular ways of uploading images in PHP

1.) Hardest method. save the image in the database as type BLOB. Load the image file either from the upload, as normal, or from another disc location. Read the file into a variable using file_get_contents() or similar. You need to pay careful attention to the type of the file, either using $_FILE[whatever][type] which is e.g. "image/gif".

Now, when you output the file, send a header("Content-type: image/gif"); and then output the contents of the BLOB. It should work fine.

2.) Easier method. Just upload the file, save it to disk, but store the filename in the database.

3.) Easy, but not easiest method. Upload the file, save it to disk using a unique identifier as filename (i.e. the auto_increment column from mysql), then save the original filename in the mysql database.


I think you are doing the hardest way (1). I would recommend you to do option 3, as it is easier and more eiffient. I think that how most people do it, including myself.


Author Comment

ID: 12480924

Hi Silversoft,

Thanks for the advice. Unfortunately, I couldn't view the thread you sent as I was unable to log on to that site, and the service required a subscription payment to get log-on details (not too cheap either).

Sorry if I am asking what may seem obvious, but with option three do I save the file to a location on the remote server (where I assume the database also is)? Anywhere in particular? Then, should I save the image using a unique filename and then save that filename in the mysql database? Would I save it as type varchar or binary or...?
Also, how does the mysql database associate the image with just a filename. Sorry for all these questions....


Very much appreciated



Accepted Solution

Silversoft earned 2000 total points
ID: 12481019
Log on to that thread?? strange, I can get into that thread, but anyway, it just a source where i got the 3 options from.

Alright let me walk ou through how option 3 is done:

1.) Set up your HTML form with something like this (enctype="multipart/form-data" is crucial, it must be there)

<form action="" method="post" enctype="multipart/form-data" name="form1">
  <input type="file" name="image">
  <input type="submit" name="Submit" value="Submit">

2.)In your upload image and save to database page, this is what you should do:

$query = mysql_query("INSERT INTO images VALUES ('','$_FILES[image][name]')"); //save the image filename into the databse

$image_id = mysql_insert_id(); //get the auto-increment id from the last record inserted into DB

copy ($_FILES['image']['name'], "my_upload_path/$image_id.jpg"); //save the uploaded file to it's destination

3.) To display the picture:

let [image_id] = 1 or 2 or 3 or whatever the unique id is

<img src = "my_upload_path/[image_id].jpg">

Hope you understand the logic of it....
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 12481179
Okay. Thanks for that. I'll try to make it work now.



Expert Comment

ID: 12481216
no prob :)

Good luck...

Expert Comment

ID: 12714049
You should base64encode whatever you put into your BLOB, so you'll maintain the integrity of the file. That what i use for my MySQL image admin script and it works great.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question