在目录中重命名 PHP 文件名

Rename PHP filename inside directory

我想更改目录 carbrands/alto/alto.php 中的文件名。我想更改为 alto_new.php 而不是 alto.php。但是如果我尝试将名称更改为

rename($old_name,$file_name);

使用此文件后,文件名发生了变化,但它不是在目录内替换 carbrands/alto,而是在目录外替换。如何解决这个问题?

rename("carbrands/alto/alto.php", "carbrands/alto/alto_new.php");

试试这个

您需要提及整个路径。

$old_name = 'carbrands/alto/alto.php';
$file_name = 'carbrands/alto/alto_new.php';
rename($old_name,$file_name);

我错过了 $filename.Now 的完整路径我使用了旧文件名和文件名的完整路径现在它可以正常工作了。

$pagename="carbrands/alto/alto.php";
$filename="alto_new.php";
$arr = explode("/", $page_name, 2);
$first = $arr[0];
$second1 = explode("/", $arr[1], 2);
$second = $second1[0];
$third = $second1[1];
$directory="$first/$second/";
foreach(glob('*.php') as $path_to_file) {
    $file_contents = file_get_contents($path_to_file);
    $file_contents = str_replace($page_name,$file_name,$file_contents);
   file_put_contents($path_to_file,$file_contents);
}
rename($directory.$third,$directory.$file_name);