PHP rename() Function
In this chapter you will learn:
- Definition for PHP rename() Function
- Syntax for PHP rename() Function
- Parameter for PHP rename() Function
- Return for PHP rename() Function
- Note for PHP rename() Function
- Example - renames a file or directory
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 j a va 2 s.co 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";
}
?>
Next chapter...
What you will learn in the next chapter:
- Definition for PHP rewind() Function
- Syntax for PHP rewind() Function
- Parameter for PHP rewind() Function
- Return for PHP rewind() Function
- Example -
- Example - Seek and rewind
Home » PHP Tutorial » PHP File Functions