473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 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 15886
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stephane | last post by:
Hello, I need to allow an end user to upload video files on a server and in the same time to put the file name and a few infos in a database. It must be very simple for the end user, and...
1
by: Amy Kimber | last post by:
Hello all, ok, I have a file upload secton to my site, two pages, one with a form and one that does the uploading.... some files upload fine, other don't an exe of 300k will upload, but a...
3
by: Amy Kimber | last post by:
Hello, I have a file upload page, and I've had it working fine, it was beautiful :-) Anyway, the powers that be moved hosts... and it doesn't work now. The file name is correct, the directory...
3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
2
by: dave | last post by:
Hello, I'm trying to get the below script working, adapted from the manual on php.net. Although i am getting success file uploading, i do not see the file in the tempoary area. And i'm not sure...
3
by: Pitcairnia | last post by:
The basic purpose of the site is for authenticated users to post event listings, which often include photographs. The user is faced with a page where they can insert all of the information about...
2
by: Simon | last post by:
I'm following microsoft's guide on how to upload files. I want to upload AND rename the file... Anyone have some tips for how to do that? Thanks ------ If Not File1.PostedFile Is Nothing And...
9
by: Steve Poe | last post by:
I work for an animal hospital trying to use PHP to store an animal's dental x-rays to a file server. I can browse for the xray on the local desktop computer then click "Upload Image". This...
0
by: xolaks | last post by:
Salutations! I'm currently developing a page that is allowing many users to upload and download their files. But the probability of big amount on requests drives me to think how to handle files with...
1
by: chennaibala | last post by:
can any one send me mutiple image upload program and save the file name with extension in mysql table.we must cheak uploaded file type like bmp or any image file while uploading. i develop...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.