Home >Backend Development >PHP Problem >How to change directory name in php
In PHP, you can use the rename() function to change the directory name. This function is used to rename files or directories. The syntax format is "rename(oldname,newname)". Returns TRUE if the change is successful; FALSE if it fails.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php change directory name
//修改目录 /* * @param editefile // 修改文件的方法 * @param jpath // 旧的文件夹名 * @param xpath // 新的文件夹名 * @param is_dir() // 判断有没有就得目录 * @param rename() // 修改目录的函数 */ function editfile($jpath,$xpath){ $_path = iconv('utf-8', 'gb2312', './Public/Uploads/' . $jpath);//旧文件名 $__path = iconv('utf-8', 'gb2312', './Public/Uploads/' .$xpath);//新文件名 if(is_dir($_path)){//判断有没有旧的目录 if(file_exists($__path)==false){ if (rename($_path, $__path))//修改目录 { $value['file']='修改成功'; $value['success']='success'; } else { $value['file']='修改失败'; $value['fail']='fail'; } }else{ $value['file']='有同级目录名相同'; $value['fail']='fail'; } }else{ $value['file']='没有找到要找的目录'; $value['fail']='fail'; } return $value; }
Related function description:
is_dir() function checks whether the specified file is a directory. Returns true if the filename exists and is a directory.
rename() function renames a file or directory. If successful, the function returns TRUE. On failure, returns FALSE.
Syntax
rename(oldname,newname,context)
Parameters | Description |
---|---|
oldname | Required. Specifies the file or directory to be renamed. |
newname | Required. Specifies the new name of the file or directory. |
context | Optional. Specifies the environment for a file handle. context is a set of options that can modify the behavior of the stream. |
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to change directory name in php. For more information, please follow other related articles on the PHP Chinese website!