Take the 2-minute tour ×

I need to get this done very quickly. What's an easy way to do image uploading in php...I have a script atm but if an image with the same name comes it'll overwrite it.

Basically I just want to be able to use the form field file to select an image and upload and maybe rename it somehow. Then put the location into a database to retrieve when needed.

Any tips or ideas on how to do this? Don't have a lot of time to get this done.

share|improve this question
What's the current code you use? Adding a simple if (file_exists($file)) and renaming if it exists should solve your overwriting problems. –  kapa May 23 '11 at 7:13
I haven't made the code yet but I know I ran into the problem a while back where it would overwrite the image...how would I go about renaming it? –  Josip Gòdly Zirdum May 23 '11 at 7:17
See something similar I've written lately: stackoverflow.com/questions/6066951/php-image-type-detection/… –  deceze May 23 '11 at 7:18

3 Answers 3


http://www.plupload.com/ - is also a good way to do this. Doing something

very quickly ... the easy way

Sounds to me like you are looking for the easy way out. Please remember that we are not here to give you your code solution, but merely as an advisory panel to help you get over some obstacles in your current (already tried to make it work) code. If you read up a bit its real easy, thats the reason for documentation on everything, especially PHP and file uploading.


share|improve this answer
doing an assignment for class, I forgot 1 element, it's due fairly soon so it needs to be fairly quick. :P –  Josip Gòdly Zirdum May 23 '11 at 7:18
I understand. I also had to do stuff for class. –  etbal May 23 '11 at 7:59

Seems to me right now, your script has a security issue as well.

Append a unique hash to the end of the filename before you save it. That way no one can overwrite your files.

share|improve this answer

Can you just append uniqid() to the filename..?

$target = "files/";

// Upload the file to directory
$url = basename($_FILES['uploadedfile']['name']);
$name = str_replace(' ', '_', $url);
$target .= strtolower($name . uniqid());

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target))
    echo 'File has been uploaded<br />
    <a href="' . $target . '">http://yoursite.com/directory/' . $target . '</a>';

Obviously still needs to be made more secure.

share|improve this answer
I am using $_FILES on another page to get it and then calling a function with the variable in it...e.g. upload($url) url being basename($_FILES['uploadedfile']['name']); The issue is when I get to move_uploaded_file($_FILES['uploadedfile']['tmp_name'] how do I do it? as I am doing it in the function... –  Josip Gòdly Zirdum May 23 '11 at 7:49

Your Answer


