By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,731 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,731 IT Pros & Developers. It's quick & easy.

PHP upload - rename uploaded file

P: n/a
Hi,
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

Regards,
Jaunty Edward

Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 14 déc. 2004, Sir sm***********@hotpop.com claimed in
news:11**********************@f14g2000cwb.googlegr oups.com:
Hi,
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

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
http://www.php.net.

Good luck.
Jul 17 '05 #2

P: n/a
Hi,
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 http://www.sfsu.edu/training/mimetype.htm

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

Jul 17 '05 #3

P: n/a
sm***********@hotpop.com wrote:
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 http://www.sfsu.edu/training/mimetype.htm

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.


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';
}
else
{
echo 'neither $a or $b were true';
}

Jul 17 '05 #4

P: n/a
Oh,
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.

Thanks.

Jul 17 '05 #5

P: n/a
<sm***********@hotpop.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Oh,
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.

Thanks.
Hi,
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.


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)

Try:

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.
Norm
--
Avatar hosting at www.easyavatar.com
Jul 17 '05 #6

P: n/a
HI,
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
else.

Thank you all of you.

Bye

Jul 17 '05 #7

P: n/a
<sm***********@hotpop.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
HI,
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
else.

Thank you all of you.

Bye


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

Norm
--
Avatar hosting at www.easyavatar.com
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.