473,807 Members | 2,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($_FILE S['userfile']['name']);
move_uploaded_f ile($_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 15916
On 14 déc. 2004, Sir sm***********@h otpop.com claimed in
news:11******** **************@ f14g2000cwb.goo glegroups.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($_FILE S['userfile']['name']);
move_uploaded_f ile($_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***********@h otpop.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.goo glegroups.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.goo glegroups.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
11135
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 uploading with a browser in a form would be perfect since it allows to upload and fill a form in the same time. I'll have full control of the server (no max_size problem etc). The end user connexion is ADSL, the server is on a 100mb internet connexion.
1
3735
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 word document of 40k wont and so on, in the php.ini a limit of 2mb is set (I haven't changed it, it's how the file originally as, so it isn't my maths that's gone mad).
3
7098
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 where it's going on the server is correct (checked through prints etc.). I've changed the dir to 777 (is that right?) and this is what I get on the print_r($_FILES); line:
3
11770
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 table in MySQL with the path & filename to the image. I have successfully uploaded and performed an update query on the database, but the problem I have is I cannot retain the primary key field in a variable which is then used in a SQL update...
2
2015
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 where to go as the server confirms file uploaded successfully. Any help appreciated. Thanks. Dave. <?php
3
3064
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 the event in a typical form with textboxes, drop down lists, etc. On this page there are also 4 file upload controls where the user can select 0 to 4 images on their file system for inclusion in the event listing. On submit, the images (if any)...
2
2624
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 File1.PostedFile.ContentLength > 0 Then Dim fn As String = System.IO.Path.GetFileName(File1.PostedFile.FileName)
9
3905
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 works fine. The doctors want fewer steps to follow. So, it was asked if I can configure the browser to load/submit the image 'xray.tif' each time they click "Upload Image" instead of the doctor/animal technician having to look for for dental x-ray...
0
970
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 their original names in the same folder. There can be files uploaded with the same name. Or is it a sensible idea to create separated folders for each file under, for instance, an "uploads" folder? I'm not sure if is there a subfolder count...
1
4887
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 program,which can upload many file in folder.problem is,am unable to save my file name in to database.because i used same name for all input file type as file. i dont know get name of file.below i presented my coding for ur view.fed up with my coding..pls...
0
9600
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10628
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10373
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9195
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5547
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.