Go Back   Dynamic Drive Forums > General Coding > PHP
Search Dynamic Drive Forums:

Reply
 
Thread Tools Search this Thread
  #1  
Old 11-07-2009, 10:53 AM
lord22 lord22 is offline
Junior Coders
 
Join Date: Jul 2008
Posts: 81
Thanks: 38
Thanked 2 Times in 2 Posts
Default rename files

Hi,

I have 2 files in my folder: 1.html and 2.html

How can I rename the second file- 2.html to 1.html (so the original 1.html file will be deleted)?

Thanks
Reply With Quote
  #2  
Old 11-07-2009, 03:36 PM
bluewalrus bluewalrus is offline
Elite Coders
 
Join Date: May 2007
Location: Boston,ma
Posts: 1,017
Thanks: 94
Thanked 82 Times in 81 Posts
Default

Where are you trying to do this, on the server, on the site, with ftp?

If your trying to do this with php, in the current directory, you can make a page and put in the following, I think.

PHP Code:
<?php
unlink
("1.html");
rename("2.html""1.html");
?>
http://php.net/manual/en/function.rename.php
http://www.php.net/manual/en/function.unlink.php

Last edited by bluewalrus; 11-07-2009 at 03:41 PM.
Reply With Quote
  #3  
Old 11-07-2009, 03:42 PM
Nile's Avatar
Nile Nile is offline
Elite Coders
 
Join Date: Jan 2008
Posts: 3,007
Thanks: 10
Thanked 442 Times in 438 Posts
Blog Entries: 5
Default

Just a little warning: Unlink() can be VERY dangerous. Be careful. Never unlink the wrong file, and never glob('*') and then unlink.

I suggest something like this:
Code:
if(!is_dir("backup")){
  mkdir("backup");
}
rename("1.html", "backup/1.html");
rename("2.html", "1.html");
This way no files are deleted. This code was not tested.
__________________
Coding is just another puzzle. And it's okay to have help putting together some peices.
Please don't be afraid to ask.
I try to highlight my code with correct syntax highlighting. Unless the its too long. Or I'm to busy.

Last edited by Nile; 11-07-2009 at 03:52 PM. Reason: Code
Reply With Quote
The Following User Says Thank You to Nile For This Useful Post:
lord22 (11-07-2009)
  #4  
Old 11-07-2009, 06:25 PM
Nile's Avatar
Nile Nile is offline
Elite Coders
 
Join Date: Jan 2008
Posts: 3,007
Thanks: 10
Thanked 442 Times in 438 Posts
Blog Entries: 5
Default

Glad to help. If yam file was ever deleted by accident, just go in the backup folder, and you should get your file back.

It seems your topic is solved... Please set the status to resolved.. To do this:
Go to your first post ->
Edit your first post ->
Click "Go Advanced" ->
Then in the drop down next to the title, select "RESOLVED"
__________________
Coding is just another puzzle. And it's okay to have help putting together some peices.
Please don't be afraid to ask.
I try to highlight my code with correct syntax highlighting. Unless the its too long. Or I'm to busy.
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:02 PM.

Home - Contact Us - Archives - Link to DD - Top 

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.