php rename函数介绍

rename — 重命名一个文件或目录

语法:

bool rename ( string $oldname , string $newname [, resource $context ] )

尝试把 oldname 重命名为 newname。 

参数:

  1. oldname 用于 oldname 中的封装协议必须和用于 newname 中的相匹配。 
  2. newname 新的名字。 
  3. context 在 PHP 5.0.0 中增加了对上下文(Context)的支持。有关上下文(Context)的说明参见 Streams。

返回值:成功时返回 TRUE, 或者在失败时返回 FALSE。 

 

PHP使用rename()函数重命名目录

使用PHP的rename()函数可以重命名目录。如果rename()函数执行成功,则返回true,否则返回false。

<html>
<head>
<  http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />
< >PHP重命名目录示例-www.manongjc.com</ >
</head>
<body>
<?php
$state = rename(\'folder\',\'folder2\'); // 将folder文件夹重命名为folder2文件夹
if($state){
  echo \'重命名目录成功!\';
}else{
  echo \'重命名目录失败!\';
}
?>
</body>
</html>

 

PHP移动目录

<html>
<head>
<  http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />
< >PHP移动目录示例-www.manongjc.com</ >
</head>
<body>
<?php
/* http://www.manongjc.com/article/1349.html */
function move($source,$dest){
  $file =  name($source);
  $desct = $dest.DIRECTORY_SEPARATOR.$file;
  return rename($source,$desct);
}
move(\"folder\",\"folder2/\"); // 将folder文件夹移动到folder2文件夹中
?>
</body>
</html>

上面的代码只能移动文件夹的位置,而不能给文件夹重命名。

 

PHP移动并重命名目录

同样,使用PHP的rename()函数可以移动并重命名目录。

<html>
<head>
<  http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />
< >PHP使用rename()函数移动并重命名目录示例-www.manongjc.com</ >
</head>
<body>
<?php
// 将folder文件夹移动到folder2文件夹中,并重命名为folder3文件夹
$state = rename(\'folder\',\'folder2/folder3\');
if($state){
  echo \'移动并重命名目录成功!\';
}else{
  echo \'移动并重命名目录失败!\';
}
?>
</body>
</html>
收藏 打印