Upload images using PHP - link primary key to image

Hello all,
I am familiar with php and mysql coding. I learned most of my php/mysql coding from the sitepoint book “how to build database driven websites using php and mysql.” Great book! Really simplified my learning. However, I have never done any coding with images.

My dilemma is first trying to figure out if the best way to upload images is to store them in a folder or in the database as a BLOB.

The next dilemma is how do I add the primary key custnum (auto-incremented) to the images that are uploaded so they can be queried later? Everything been going smoothly until I ran into this image upload issue.

Thank you in advance.

Generally speaking you store images on the file system, not in databases - but you store its address in your database. (Do some reading on the subject if you like : [google]when should images be stored in a database[/google])

That address might well become “/images/23.jpg” which is what you seem to be alluding to.

On successful upload, you would a) move the image from the /tmp directory and b) store it somewhere on your file system.

All you need to do between those operations is to enter a new row into your table - perhaps with some other descriptive text from the upload operation? the file type (jpg, bng, gif etc) or the date of upload maybe?

As the new entry is made you ask mysql for the last inserted id number, which you allocate as the file name.


One problem you may run into over time is that you cannot tell what an image contains without constantly referring to the table.


may be less helpful than:


But generally in situations such as text articles containing an image mean you end up with article id 23 may have a matching image 23 or not.

You’d have to explain more about your particular situation if you want more detailed discussion.

Hi, thank you for your response.

So far, I have the images being stored in the upload_images folder. The pics are being named whatever the user had them named, which is fine. I am just confused as to how I label the pictures the user number that mysql auto generates.
I would like for the user to enter their info, upload their pictures (up to 12), resize them to 500 pixels wide, then somehow stamp the pictures with the mysql_last_insert_id. I like the method of storing the location in the database. But at some point, I need to be able to search by user number to pull up all the users pictures.

Currently, the webpage inserts the user info to the user table. Then I pull the unique user number from the user table. I want to figure out how I label the uploaded pics with the last_id from the user table.

Thank you for your help.

Well, as the image names have to be unique you could retain the image names (you say you are happy with this) then prepend the user id number.

ie user 23 :


Either that or construct a folder for each new user:


From your description I am guessing that you create the new user account and store all the images at the same time - is that so?

If so you create the new users, grab the new users id using last insert id and then store/rename the images accordingly.

There seems an awful lot to go wrong here though, I’d consider doing it in 2 stages.

Create new user account, then direct to the upload form and have a hidden field in the upload form containing the user id:

<input type=hidden name=user_id value=23 />

and simply grab $_POST[‘user_id’] as you do the upload and add it to the path as I described above.

This makes perfect sense. Adding a date would also help distinguish images.