472,146 Members | 1,288 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

PHP upload - rename uploaded file

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
7 15802
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
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
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
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
<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
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
<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.

Similar topics

5 posts views Thread by Stephane | last post: by
1 post views Thread by Amy Kimber | last post: by
3 posts views Thread by Amy Kimber | last post: by
3 posts views Thread by dave | last post: by
2 posts views Thread by dave | last post: by
3 posts views Thread by Pitcairnia | last post: by
2 posts views Thread by Simon | last post: by
9 posts views Thread by Steve Poe | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.