Rename PHP filename inside directory

246 views Asked by At

I want to change filename in directory carbrands/alto/alto.php .Instead of alto.php I want to change as alto_new.php. But if I try to change name as

rename($old_name,$file_name);

After using this the filename changed but its not replace inside directory carbrands/alto instead its replaced out of directory. How to fix this issue?

3

There are 3 answers

0
Mihir Chhatre On BEST ANSWER

You need to mention the entire path.

$old_name = 'carbrands/alto/alto.php';
$file_name = 'carbrands/alto/alto_new.php';
rename($old_name,$file_name);
1
M A SIDDIQUI On
rename("carbrands/alto/alto.php", "carbrands/alto/alto_new.php");

try this

1
Kavya Shree On

I missed full path for $filename.Now I used full path for old and filename Now its worked correcly.

$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);