PHP rename() Function
Definition
The rename() function renames a file or directory.
Syntax
PHP rename() Function has the following syntax.
rename(oldname,newname,context)
Parameter
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. |
Return
This function returns TRUE on success, or FALSE on failure.
Note
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.
Example
renames a file or directory
<?PHP/*from w ww. j a v a 2 s. c om*/
$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";
}
?>