1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Rep Power

    Unhappy Handling images using PHP With MySQL


    I'm nervous about completing my clients web-application, a mysql database and administrative user interface to manage that database - where all changes would be directly apparent on the public web-site; because I have no idea how to handle images in mysql and php, you can see why I'm nervous.

    I've done some research and I think I should store the images on the web server, and put the link to them in the database within its respective row.

    However, I have no idea how to generate the proper file names, paths, and the user "Browse for files on computed" upload function.

    Does anybody have any pointers to get me started - I'm fairly novice in the implementation of the php and mysql code, maybe intermediate in my understanding of computers and programming.
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Pennsylvania, USA
    Rep Power
    1) Uploading files in an HTML form. Use <input type="file"> and you're done.

    2) Accepting file uploads in php. See the manual for move_uploaded_file. Make sure the destination is still inside the web root somewhere

    3) Naming the destination file. See the manual for uniqid

    4) Inserting the file row into the database. You already know this for the rest of your operations, a file path is no different than any other string.

    5) Showing the images back to end-users. Print the <img /> tag like any other image, just use the filename in the database. As long as you put it in a web-accessible directory in #2 you should be fine.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Rep Power
    From a security standpoint, the most critical element is validating the extension of the uploaded file. The manual page on move_uploaded_file should discuss this.

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Rep Power
    This isn't a solution but let me give you a little advice...

    If you are talking about a few hundred images, it might be ok to store them in the database. But more than that and you are creating a bottleneck in the system.

    Keeping track of image names in the database but keeping the files on the hard drive is more efficient. I say this from experience on several projects where image storage was in the database at first for ease of access. Over time, getting the images required more and more resources and a lot of database traffic.

    In every case, we ended up moving all images off the database and into the file system.

    Some times we check for the existence of the image every time it is asked for other times only once in a while just in case someone manually deleted it. In most cases we have a Javascript function that fires off when an image can't load to notify our app to go check it and at the same time put up a "no image" image for the user.

    If you really need to store the images in the database then so be it. I would just give that some serious thought.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Rep Power
    A head start

    PHP Code:
    $images mysql_query("SELECT img_href FROM images");

    $row mysql_fetch_array($images)){
    '<img src="' $row['img_href'] . '" />';


IMN logo majestic logo threadwatch logo seochat tools logo