note 72164 added to function.rename
| From: | (dev@islam-soft.com) | Date: | Fri, 05 Jan 2007 12:55:01 +0000 |
| Subject: | note 72164 added to function.rename | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-121314@lists.php.net to get a copy of this message | ||
- 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)) { # Look at only files with a pre-defined extension
$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 ''
devloped by : Mohamed Almasry - Islam Soft - Cairo
www.islam-soft.com
----
Server IP: 66.163.161.117
Probable Submitter: 196.219.143.152
----
Manual Page -- http://www.php.net/manual/en/function.rename.php
Edit -- https://master.php.net/note/edit/72164
Del: integrated -- https://master.php.net/note/delete/72164/integrated
Del: useless -- https://master.php.net/note/delete/72164/useless
Del: bad code -- https://master.php.net/note/delete/72164/bad+code
Del: spam -- https://master.php.net/note/delete/72164/spam
Del: non-english -- https://master.php.net/note/delete/72164/non-english
Del: in docs -- https://master.php.net/note/delete/72164/in+docs
Del: other reasons-- https://master.php.net/note/delete/72164
Reject -- https://master.php.net/note/reject/72164
Search -- https://master.php.net/manage/user-notes.php