- rename extension of files
changeext($directory, $ext1, $ext2, $verbose)
i wrote this function to rename the extention of some files in a folder and sub-folders inside it ..
parameter 1 : the directory name
parameter 2 : the first extention wich we want to replace
parameter 3 : the new extention of files
for a simple usage call the function :
changeext('dir', 'html', 'php', 'false');
to change evry file name with extention html into php in the directory dir
<?php
function changeext($directory, $ext1, $ext2, $verbose = false) {
$num = 0;
if($curdir = opendir($directory)) {
while($file = readdir($curdir)) {
if($file != '.' && $file != '..') {
$srcfile = $directory . '/' . $file;
$string = "$file";
$str = strlen($ext1);
$str++;
$newfile = substr($string, 0, -$str);
$newfile = $newfile.'.'.$ext2;
$dstfile = $directory . '/' . $newfile;
if (eregi("\.$ext1",$file)) { $fileHand = fopen($srcfile, 'r');
fclose($fileHand);
rename($srcfile, $dstfile );
}
if(is_dir($srcfile)) {
$num += changeext($srcfile, $ext1, $ext2, $verbose);
}
}
}
closedir($curdir);
}
return $num;
}
changeext('dir', 'html', 'php', 'false');
?>
to remove the extention of files , just leave the parameter $ext2 blank ''