Log in

View Full Version : rename files



lord22
11-07-2009, 10:53 AM
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 :)

bluewalrus
11-07-2009, 03:36 PM
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
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

Nile
11-07-2009, 03:42 PM
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:


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.

Nile
11-07-2009, 06:25 PM
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"