PHP upload - rename uploaded file

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have to allow my visitors to upload image on my site.

I am using the follwoing code to do that:

$uploaddir = 'admin/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);

My problem is that I expect them to use same file name, like all of
them will be uploading files like, comments.rtf. So if there is already
one comments.rtf the file will get overwriten... so the challenge is to
rename the files to username-filename.rtf

can anybody here will take the pain to modify this code, I will be
thankful, or please guide to me some tutorial where they are dealing
with simple code and not with advanced classes.
Thank you in advane

Jaunty Edward

Re: PHP upload - rename uploaded file

On 14 déc. 2004, Sir claimed in

Quoted text here. Click to load it

In your code there is a line where you are storing the real name of the
file in $uploadfile (that you should have named $uploadedfile), so the
action you have to take is on that line...

You should learn to program in PHP, this is a very easy language, and the
better way to learn is to read the PHP manual from the very beginning at .

Good luck.

Re: PHP upload - rename uploaded file

I fixed it with someone's help, now next problem is I am using
($_FILES['userfile']['type'] == "image/gif") to check if the file is
gif, its working but if I change the mime type to jpeg or png or
anything else its not working.

i took the mime types from

so if I use $_FILES['userfile']['type'] == "image/png" it does'nt work
not with jpeg and jpg also I don't know what to do.

Please help

Re: PHP upload - rename uploaded file wrote:
Quoted text here. Click to load it

This is really basic stuff you are asking here. You should lookup the
logical operators in the manual:

if($a || $b)
  echo '$a and/or $b is true';
  echo 'neither $a or $b were true';

Re: PHP upload - rename uploaded file

you are getting me wrong. I am not asking for if else loop or anything,
what i am talking about is only one file type.

if I use only gif it works, if I only use jpeg or png it does'nt.

SO the question is not if else, I only want to use one mime type, I
will handle the rest.


Re: PHP upload - rename uploaded file

Quoted text here. Click to load it

1) What mime type (file type) doyou want to allow?
2) How are you handling the file if it is not what you want?
3) For testing purposes, try 'echoing' the $_FILES['userfile']['type'] to
the screen to see what it is (and why it isn't working)


echo "\n\r <!-- INFO: mime type of uploaded file is:
$_FILES[userfile][type] --> \n\r";

This will print in your HTML output, not directly on the screen, so you will
have to 'View Source' from your browser to find it.

Avatar hosting at

Re: PHP upload - rename uploaded file

thank you Norman, after looking at the echo I realised that the file
type should be image/x-png and not image/png....

Now I will find all extensions with the echo...and then use them in if

Thank you all of you.


Re: PHP upload - rename uploaded file

Quoted text here. Click to load it

That mime-type list is incorrect (a least as far as png goes).

Avatar hosting at

Site Timeline