Home >Backend Development >PHP Tutorial >RenameChange file and folder names_PHP tutorial
The rename() command can rename files and folders, and can also move files and folders. The command format is:
bool rename ( string oldname, string newname [, resource context] )
The following demonstrates the specific application of rename:
The file location is as shown:
Purpose: 1. Rename cache.txt to rename.txt;
2. Rename cache2.txt to cache3.txt
3. Rename the html directory to cache
4. Move the file directory to the html directory (rename can be achieved)
Code implementation (with errors):
$file = "html/cache.txt";
$rename = " html/rename.txt";
if(rename($file,$rename)){
echo "Name change successful";
}else{
echo "Name change failed";
}
rename("html/cache2","html/cache3.txt");
rename("html","cache");
rename("file","html/files");
?>
Common error analysis:
Check the syntax and there is no problem; after checking, the file directory does not exist, causing an error. More files were compiled successfully
This mistake is small but fatal………………