This article analyzes the usage of the rename() function in PHP with examples. Share it with everyone for your reference. The details are as follows:
php filesystem function, rename() function renames a file or directory. If successful, the function returns true. If it fails, return false.
Statement:rename(oldname,newname,context)
参数 | 描述 |
oldname | 必需,规定要重命名的文件或目录. |
newname | 必需,规定文件或目录的新名称 |
context | 必需,规定文件句柄的环境,context 是可修改流的行为的一套选项 |
Note: Prior to php 4.3.3, rename() could not rename files across disk partitions on *nix-based systems.
Note: The encapsulation protocol used in oldname must match the one used in newname.
Note: Support for context was added in php 5.0.0.
oldpath ---- the original path of the file or directory, $newpath ---- the new defined path, then rename($oldpath,$newpath) can complete the file/directory moving operation. After my testing, win32 and All php4 versions of unix support this function.
In addition, it seems that the win32 version of php4 has canceled the unlink() function, so you can also use the rename() function to complete the deletion operation, for example:
$path ---- file or directory path
$tmp ---- tmp directory (/tmp)
Use rename($path,$tmp) to move the file to the tmp directory.
I hope this article will be helpful to everyone’s PHP programming design.