473,670 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP File upload - rename 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
2 7366
I noticed that Message-ID:
<11************ **********@f14g 2000cwb.googleg roups.com> from
sm***********@h otpop.com contained the following:

$uploaddir = 'admin/';
$uploadfile = $uploaddir . basename($_FILE S['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


Is the username unique? If so it's just

$uploadfile = $uploaddir.
$username.basen ame($_FILES['userfile']['name']);

Though you might like to add a character for neatness

$uploadfile = $uploaddir .
$username."-".basename($_FI LES['userfile']['name']);

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #2
Hi,
thank you so much Geoff Berrow its worked infact now I can use many
ways to changing name of file.

Thank you.

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3729
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
7089
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:
18
9095
by: Dino | last post by:
dear all, i've created an application for a customer where the customer can upload ..csv-files into a specified ftp-directory. on the server, a php-script, triggered by a cronjob, reads all the data, imports it into a mySQL database and deletes the .csv-file after the import. so far, so good. but in some cases the cronjobs starts running when the file is not completely
2
3387
by: Keith | last post by:
Is it possible to rename a file which a user uploads to the server? I have a form which allows users to upload an image to a folder on the web server. This works fine. The form they submit takes their firstname, surname and the file. However, I want the file to automatically be renamed to firstname_surname.jpg (taking their firtname and surname from the form they are submitting).
2
2619
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)
4
4475
by: Matt Jensen | last post by:
Howdy I've got a rather strange issue occuring. I used forms based .NET authentication, although I'm also setting some session variables when people login. However, I've found when people use one of my webform pages which includes a button that pops up a window where you can upload files, if you upload files in this popup window, it seems to somehow clear out all of the session variables and the users get logged out. However, if...
2
4254
by: will.smothers | last post by:
All, I have two Windows Server 2003 boxes running IIS 6.0 webservers that host our proxy pac file. These two servers are behind a network load- balancer and house our corporation's Proxy PAC file. I am working on developing an ASP-based file upload solution for our Proxy team so that when they make a change to the PAC file, they simply have to go to this upload site, browse for the new PAC file on their system and then upload it to the...
4
6902
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the function creates the file as expected. When I loop through my array I get the error - "ArgumentException was unhandled - Illegal characters in path" The value "C:\Temp.txt" is the first value in the array - as it works
6
3832
Jacotheron
by: Jacotheron | last post by:
I need a PHP script that can upload music files (mp3). The script is for a home project I have started a while ago. I have a MySQL database of all the music that I have. Other computers on the network should be able to connect to the database and run queries on the database or upload new music that does not yet exist on the database. The uploaded file's name should be in the following format: ARTIST - TITLE.mp3. I have the code to upload images,...
4
5260
by: PHPstarter | last post by:
Hi guys. I'm having a problem with this case. I want a php script activated by a standard file submit to: 1) upload the file to ../../upload 2) copy the file to a directory further back, for example .././latest/../.. 3) rename the file, no matter what name it has - to play.html So far I got the upload working and the file is uploaded to the folder /upload.
0
8466
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8384
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
8901
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...
1
8591
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8659
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7412
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...
0
4208
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...
1
2799
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
2037
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.