Current location: Hot Scripts Forums » Programming Languages » PHP » Rename file


Rename file

Closed Thread
  #1 (permalink)  
Old 05-16-06, 05:06 AM
Jack Stone Jack Stone is offline
Newbie Coder
 
Join Date: Nov 2005
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Rename file

Hi

I think what I need is just a line or 2 of php code

This is my situation

filename.jpg gets uploaded and if it exists then rename to filename_($n).jpg
$n being a number so if filename_1.jpg also exists then name the file filename_2.jpg

If anyone understands what I need, help will be appreciated.

Regards...
Jack Stone
  #2 (permalink)  
Old 05-16-06, 05:18 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
You might want to have a look at this

rename();
file_exists();
  #3 (permalink)  
Old 05-16-06, 06:14 AM
bertrand bertrand is offline
Newbie Coder
 
Join Date: May 2006
Location: Bruges, Belgium
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
I do something similar on one of my websites, here is the code:
PHP Code:

 // Check if the file exists
if (file_exists ("downloads/$filename") == true) {
    
// If it exists, we're going to put "[1]" in front of it, if that already exists "[2]", ...
    
for ($i 1$i $i$i++) {
     if (
file_exists ("downloads/[$i$filename") == false) {
     
$filename "[$i$filename";
     break;
     }
    }
}
// Here you can use move_uploaded_file() 
__________________
Never do anything you wouldn't want to explain to the paramedics.
  #4 (permalink)  
Old 05-16-06, 04:19 PM
Barnz1986 Barnz1986 is offline
Aspiring Coder
 
Join Date: Jan 2006
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
its like: -

rename("your-old-file-location-and-name.php","your-new-file-location-and-name.php");
  #5 (permalink)  
Old 04-24-07, 03:24 PM
samot samot is offline
Newbie Coder
 
Join Date: Apr 2007
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Why don't you use radom name, so you don't have to deal with name duplication. Just a thought.


Quote:
Originally Posted by Jack Stone View Post
Hi

I think what I need is just a line or 2 of php code

This is my situation

filename.jpg gets uploaded and if it exists then rename to filename_($n).jpg
$n being a number so if filename_1.jpg also exists then name the file filename_2.jpg

If anyone understands what I need, help will be appreciated.

Regards...
Jack Stone
  #6 (permalink)  
Old 04-24-07, 03:27 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Please don't bump old topics... this is a year old.
Closed Thread

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with my largest perl script. Grabbing data from a file. Sunnmann Perl 2 04-23-08 03:27 AM
Upload a file to a fix web folder of a website without using FTP software sujata_ghosh Perl 4 05-10-06 09:10 AM
Error message not getting displayed. sanjeet Windows .NET Programming 0 12-20-05 10:48 AM
Getting the created file (fopen/fwrite) Programme PHP 5 02-14-04 03:09 PM
Upload file type and size limiter! Arctic ASP 1 08-02-03 07:06 PM


All times are GMT -5. The time now is 07:12 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.