The rename() function renames a file or directory.
PHP rename() Function has the following syntax.
rename(oldname,newname,context)
Parameter | Is Required | Description |
---|---|---|
oldname | Required. | File or directory to be renamed |
newname | Required. | New name of the file or directory |
context | Optional. | Context of the file handle. |
This function returns TRUE on success, or FALSE on failure.
The rename() function should be used to move ordinary files, and not files uploaded through a form.
move_uploaded_file() checks to make sure the file has indeed been uploaded before moving it.
renames a file or directory
<?PHP/* w w w . j a v a 2 s. c o m*/
$filename = "c:/abc/test.txt";
$filename2 = $filename . '.old';
$result = rename($filename, $filename2);
if ($result) {
print "$filename has been renamed to $filename2.\n";
} else {
print "Error: couldn't rename $filename to $filename2!\n";
}
?>