Can someone tell me how to make an image upload script?
Hey,
I have looked everywhere for a simple image upload script but they are all different and I cannot tell what code is needed and what code is not. I want to allow users on (who are logged in) to upload and image to their profile but I am confused about the database part too for example, the field where the image is stored needs to be a short blob etc
Also, some people say its better to save a url of an image because it takes up less room?
If anyone can help please can you provide a basic script that allows the user to upload an image into the database (I can prob code it such that it is stored in the approriate user record in the database)
Any help will be appresiated!
greg posted this at 15:53 — 2nd November 2007.
He has: 1,581 posts
Joined: Nov 2005
Is it only one image per user?
if yes, and I presume duplicate usernames are not allowed, then one option is ....
create a subfolder on the server called user_images (or whatever)
when the user uploads an image, have the PHP create a sub-folder on the server in the "user_images" folder
name it the users username
then name the filename the user name
I'm guessing where the image is going, but if say it's for a forum for an avatar, when the avatar is drawn make the
that isnt exact but gives you the general idea
so you dont need DB references at all
and the script for that is very simple
a form to upload a file, the page the form goes to simply creates a folder with the username and a file with the username
you will of course on the form process page need to check for a few things
you will want to limit file sizes, and perhaps have some sort of rescaling of the images, again i don't know what you really want it for
word of warning though, if you anticipate many users uploading images, that folder (user_images) will start to have many sub folder in it
It's not good practice to have too many folders in one folder
so then you might want to create sub folders by something else
perhaps users register date
so have the script create a sub-folder for each month, or sets of 3 or 4 months
this all depends on how many users you have and how many of them you expect to upload images
if you give more info about the site and what/where the images are to be used I can suggest a more precise option for your circumstance and maybe provide some code if you can't do it yourself from scratch
drew22299 posted this at 10:32 — 5th November 2007.
They have: 105 posts
Joined: Mar 2006
Thanks for your quick reply, the folder idea sounds good but as you said if there are alot of users it might become slow. At the moment its a basic site where people register and have their own profile, other members can send messages to each other, add to their friends list and browse members. I just need to allow users to upload one pic, which will appear on their profile and maybe on other pages such as in search results.
With the folder idea will the image be overwritten when the user uploads a different pic every time? (since they are only allowed one pic this is what I need)
Do you know how to insert pics into a database? I'm guessing that would be more secure? and quicker if there were alot of pics?
greg posted this at 14:27 — 5th November 2007.
He has: 1,581 posts
Joined: Nov 2005
I wouldnt save the images into MYSQL
It's not a problem as such, I just think it's better to be on the server hard drive as a standard 'file in folder'
to be honest me giving you the best option for your site is too hard, I dont know how your site works, how many members etc
a good all round method is :
the end result on your server will be this:
/user_images / year_month / username / file
make a new folder on your server..."user_images" (whatever)
FILENAME:
you can use the filename of the image they are uploading
FOLDER
make a new field in the DB called "foldername"
this is going avoid you having many sub_folders in a single folder
SO
when user goes to the upload form and chooses an image and clicks GO
script checks the server to see if the folder exists
$date = current month and year
(result has to look like this nov2007)
IF EXISTS - /user_images / $date / $username /
delete the current file from in there (as you only allow one file) then write the new file to it
(it did exists so there is a file there and they want to overwrite with a new image, so delete current one and write new, and of course there will be a db reference)
ELSE
create the folder name it $date, then write the file to it, then write that $date into the users row in fieldname "foldername" in the DB
when you come to display the users image
query the DB, get the row where the username matches the username, select the "foldername" field
then
$file_location = /user_images / $row['foldername'] / $username
get the file from in there (there should only be one) and display it
NOTE
You could create the folders manually, it sounds like a pain, but creating 12 folders (one for each month) and you are set for a year
I suggest that, just incase on the offchance that two people are creating a file at the same time
both hit the server at same time and folder doesnt exist (eg dec2007)
so one makes the folder, then the next one tries to make folder and error already exists
and if you do this, make sure on the db check and server check if the folder exists you MUST have the server check first
if you have made the folder manually, it wont be in the users DB "foldername"
so if DB check is first, it will check DB, not find anything and then try to make the folder, which already exists as you made it
you could allow users to choose their own filename, but you will need to get the extension of the file first so you can merge their chosen filename with the extension and then save it
again it's really up to you to decide whats best for your site, if you only have a handful of members you dont need a folder naming system as there wont be many folders/files
and without the DB reference is faster, as there is no db query it simply goes straight to their folder- so use my last post example
you will also need other checks too
max filesize - whatever you want to allow max to be
you also only want to allow certain types of extension...gif, jpg etc
and really a MUST is to check the mime type
as exploits can be made easily without checking if the file really is an image
someone could make a php exploit script file, save it as a jpg and upload it
there are some ways around it even then, but this makes it a lot harder
drew22299 posted this at 13:54 — 14th November 2007.
They have: 105 posts
Joined: Mar 2006
Ok I have a basic file upload script, but still not sure how to do the folder thing you described, also this doesn't seem to work does there need to be some sort of permissions that allow files to be uploaded? Also, where does allowing only JPEG, GIF etc come into this code?
<?php
if($_POST[upload] == "1")
{
$to = "uploads/".$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $to);
echo "Uploaded";
}
?>
LosKas posted this at 18:50 — 10th December 2007.
They have: 5 posts
Joined: Dec 2007
There are a lot of good Imageshack.us clones and such on the net. SOme are paid, some are free. I once had one really good, but decided to not create that site..
I see that you want to code it yourself, but ready made ones would be good too..
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.