View Full Version : PHP image upload script
01-28-2012, 09:58 PM
This isnt so much a problem, its more of a question, and i would like peoples opinions.
I would like a php image upload script that only allowes gif and jpeg images under 1mb in size to be uploaded, however once uploaded i would like it to add a username of the person that uploaded it and a user can only upload 1 image, so the real question is, does anyone have any tutorials/can anyone direct me to a good tutorial or a preffered tutorial.
01-29-2012, 02:44 AM
You will need some sort of database to keep track of who uploaded the image.
So, there would also need to be a user registration portion so they log-in to upload.
Not sure how much you already have in place?
01-29-2012, 03:05 AM
The log in and registration part of my site is fine :) I just wanted a simple script that will allow a logged in user to upload a single image per user, so that I can later design a photo album type page that will display them all :)
01-29-2012, 05:46 AM
Since you have a login system, you must have a MySQL table created
that contains your user information? Is that correct?
If so, add a new column to that table called "userPhoto" and then,
when a user uploads a photo, the photo goes into a directory you have
for photos and that column will contain the filename of their photo.
If their column contains a filename, they can't upload one anymore.
You could allow them to change it though?
01-29-2012, 11:53 AM
Yep I can do that, however, its more of a photo album of players and they get a prize for uploading a valid picture, therefore there is no need for them to be able to change it.
also, with the column "userPhoto" should i use varchar? if so, what should be the max size?
01-29-2012, 04:03 PM
You'll be saving the filename in your table, not the actual image.
So, varchar(80) would be fine. The image itself will
be saved in a directory that you create ... just for
all of the those images.
No matter what they upload, you can alter the dimensions
of it before saving it, and also assign your own filename to it.
The upload script ... and there are hundreds of them, can
be a cool AJAX script, or a simple no-frills. Find them via
Google (PHP file upload script).
01-29-2012, 05:41 PM
Most secure solution: Rename the image to a random string and salt it with the user's username (or use a more complex hashing algorithm) then save the hash in the database with their user id. Now you can call the user id, retrieve their image hash, and generate the URL to display the image in your PHP script. The only other thing is that you'd have to also save the image extension, or you could create a function to find any image with that name, since they'll all be uniquely named. This is the method I use in my database.
This also saves you space in your DB, since you theoretically only have to store 16-32 characters depending on how long your hashes are.
The other solution would be to just name the file as their username, so you don't have to store anything in the database. That could lead to predictable file names though, so if someone found the URL, they could just swap in any username and see that picture, not sure if that's a problem though.
01-29-2012, 07:21 PM
i get what you mean, however the users name will be displayed underneath the image they have uploaded as it will be in the photo album. thanks for all your help guys, im just looking into some scripts now :)