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

Posted on 2004-11-02
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:ayounis
    LVL 2

    Expert Comment


    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


    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


    LVL 2

    Accepted Solution

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

    Author Comment

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


    LVL 2

    Expert Comment

    no prob :)

    Good luck...

    Expert Comment

    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

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now