Location: PHPKode > scripts > Rename > rename/class.rename.php
<?php

// class to rename all the file found in a 
// directory with the name pas in parameter
class RenameFile
{
	// find directory content
	function RenameFileInDir($dir, $i, $name)
	{
		$i++;
		$x=0 ;
		if(@$checkDir = opendir($dir))
		{
			$cDir = 0;
			$cFile = 0;
			
			// check all files in $dir, add to array listFile
			while($file = readdir($checkDir))
			{
				if($file != "." && $file != "..")
				{
					if(is_dir($dir . "/" . $file))
					{
						$listDir[$cDir] = $file;
						$cDir++;
					}
					else
					{
						$listFile[$cFile] = $file;
						$cFile++;
					}
				}
			}

			// rename files
			if(count($listFile) > 0)
			{
				sort($listFile);
				
				for($k = 0; $k < count($listFile); $k++)
				{
					$spacer = "";
					
					for($l = 0; $l < $i; $l++)
						$spacer .= "&emsp;";
					
					// get the extention of the file	
					$ext = explode(".", $listFile[$k]) ;
					// display what it does
					print("Renaming : " . $dir . "/" . $spacer . $listFile[$k] . " ------> " . $dir . "/" . $name . ($k+1) . "." . $ext[1] . "<br>") ;	
					//rename the file
					if(@rename($dir . "/" . $spacer . $listFile[$k], $dir . "/" . $name . ($k+1) . "." . $ext[1]))
						$x++ ;
					else
					{
						// display message if error append 
						print("<font color='red' size='3'><b>Could not rename --> " . $dir . "/" . $spacer . $listFile[$k] . " --in--> " . $dir . "/" . $name . ($k+1) . "." . $ext[1] . " File exists !!</b></font><br>") ;
					}
				}
			}
			// closing directory
			closedir($checkDir);
			
			print("<br><br>" . $x . " files renamed correctly...<br>") ;
		}
		else
		{
			// display message if error append 
			print("<font color='red' size='3'><b>No directory found $dir</b></font>") ;
		}
	}

}

?>
Return current item: Rename