3

new php programmer here. I have been trying to rename all the files in a folder by replacing the extension.

The code I'm using is from the answer to a similar question on SO.

if ($handle = opendir('/public_html/testfolder/')) {
while (false !== ($fileName = readdir($handle))) {
    $newName = str_replace(".php",".html",$fileName);
    rename($fileName, $newName);
}
closedir($handle);

}

I get no errors when running the code, but no changes are made to the filenames.

Any insight on why this isn't working? My permission settings should allow it.

Thanks in advance.

EDIT: I get a blank page when checking the return value of rename(), now trying something with glob() which might be a better option than opendir...?

EDIT 2: With the 2nd code snippet below, I can print the contents of $newfiles. So the array exists, but the str_replace + rename() snippet fails to change the filename.

$files = glob('testfolder/*');


foreach($files as $newfiles) 
    {

    //This code doesn't work:

            $change = str_replace('php','html',$newfiles);
    rename($newfiles,$change);

           // But printing $newfiles works fine
           print_r($newfiles);
}
Community
  • 1
  • 1
Munner
  • 51
  • 1
  • 1
  • 8
  • Possible duplicate of [Bulk Rename Files in a Folder - PHP](https://stackoverflow.com/questions/4993590/bulk-rename-files-in-a-folder-php) – G_real Sep 06 '19 at 21:32

5 Answers5

11

Here is the simple solution:

PHP Code:

// your folder name, here I am using templates in root
$directory = 'templates/';
foreach (glob($directory."*.html") as $filename) {
    $file = realpath($filename);
    rename($file, str_replace(".html",".php",$file));
}

Above code will convert all .html file in .php

Nono
  • 6,986
  • 4
  • 39
  • 39
7

You're probably working in the wrong directory. Make sure to prefix $fileName and $newName with the directory.

In particular, opendir and readdir don't communicate any information on the present working directory to rename. readdir only returns the file's name, not its path. So you're passing just the file name to rename.

Something like below should work better:

$directory = '/public_html/testfolder/';
if ($handle = opendir($directory)) { 
    while (false !== ($fileName = readdir($handle))) {     
        $newName = str_replace(".php",".html",$fileName);
        rename($directory . $fileName, $directory . $newName);
    }
    closedir($handle);
}
Telgin
  • 1,614
  • 10
  • 10
  • Hi Telgin, thanks for your answer. Unfortunately I tried it out and same result, code generates no errors, but no changes either. – Munner Aug 14 '12 at 15:21
  • @Munner Try checking the return value of rename. It supposedly returns false if it can't rename the file. That would help narrow down the problem. – Telgin Aug 14 '12 at 15:28
  • Code is working fine, one little tweak: place this in the whitle loop to exclude current and parent folder: `if($fileName == "." || $fileName == "..") continue;` – hlorand Dec 12 '20 at 09:16
  • Didn't work for me. No errors but no change in filename either. – Azamat Nov 13 '22 at 18:21
0

Are you sure that

opendir($directory)

works? Have you checked that? Because it seems there might be some Document Root missing here...

I would try

$directory = $_SERVER['DOCUMENT_ROOT'].'public_html/testfolder/';

And then Telgin's solution:

if ($handle = opendir($directory)) { 
    while (false !== ($fileName = readdir($handle))) {     
        $newName = str_replace(".php",".html",$fileName);
        rename($directory . $fileName, $directory . $newName);
    }
    closedir($handle);
}
m7o
  • 901
  • 5
  • 7
  • Thank you so much for the suggestions. I've been trying a bunch of solutions with no luck so far, I'll try the above edits and let you know! – Munner Aug 15 '12 at 16:33
0

That happens if the file is opened. Then php cannot do any changes to the file.

0
<?php
$directory = '/var/www/html/myvetrx/media/mydoc/';
if ($handle = opendir($directory)) { 
    while (false !== ($fileName = readdir($handle))) {
        $dd = explode('.', $fileName);
        $ss = str_replace('_','-',$dd[0]);
        $newfile = strtolower($ss.'.'.$dd[1]);
        rename($directory . $fileName, $directory.$newfile);
    }
    closedir($handle);
}
?>

Thank you so much for the suggestions. it's working for me!