Change Filenames to Upper or Lower Case (in bulk) in PHP

Atlast I overcame my laziness and wrote a long overdue script to change filenames. Before writing this code, I couldn’t find a reasonable solution in PHP despite a major search operation. The need, infact, arose when I copied my PHP code from windows to linux machine, and it refused to show alot of images, which never gave any problem in windows as windows isn’t case sensitive. Here’s the script to change filenames from lower case to upper case and vice versa.


//$DIRNAME = "/srv/www/htdocs/kbs/pollimages" ; // for linux
$DIRNAME = "C:\\gohar\\gohar data\\ngc\\ngckbs\\pollimages" ;
$TO = "LOW" ; // UP or LOW
$SPECIAL_CHAR_REPLACE = "_" ; // To replace the special characters with
$REMOVE_SPECIAL_CHAR = true ; // will replace special characters with the value given in $SPECIAL_CHAR_REPLACE, if set to true
$REMOVE_REDUNDENCY = true ; // will remove repetitive $SPECIAL_CHAR_REPLACE, if set to true
$CONCAT = "__x_x__" ; // for temporarily replace 

$dir = dir($DIRNAME);
while (false !== ($old_filename = $dir->read())) {
	if(filetype($DIRNAME."/".$old_filename) == "file"){
		// change case
		$new_filename = ($TO == "UP" ? strtoupper($old_filename) : strtolower($old_filename)) ;
		// replace special characters
		if($SPECIAL_CHAR_REPLACE){
			$new_filename = ereg_replace("[^a-z0-9.]" , $SPECIAL_CHAR_REPLACE , $new_filename);
		}
		// remove repetition after replacement
		if($REMOVE_REDUNDENCY){
			$new_filename = ereg_replace("[$SPECIAL_CHAR_REPLACE]{1,}" , $SPECIAL_CHAR_REPLACE , $new_filename);
		}
		// remove replacement from end
		if(substr($str , -1) == $SPECIAL_CHAR_REPLACE){
			$new_filename = substr($new_filename , 0 , -1);
		}
		// first rename
		@rename($DIRNAME."/".$old_filename, $DIRNAME."/".$CONCAT.$new_filename) ;
		// second rename
		@rename($DIRNAME."/".$CONCAT.$new_filename, $DIRNAME."/".$new_filename) ;
		echo $DIRNAME."/".$new_filename."<br>" ;
	}
}
$dir-&gt;close();

$CONCAT is required because in windows, atleast in my case, it doesn’t change the case of file if the filename is to be kept the same, as it doesn’t consider it a different filename. So I had to rename it using $CONCAT and then remove this concatenation. This gave me a filename with changed case, thus changing the filename to lower case or upper case. This script also removes special characters and replaces those with any mentioned replacement in $SPECIAL_CHAR_REPLACE and then removes redundency of replacement character.

Hope it serves someone’s purpose.

Posted in PHP.

3 thoughts on “Change Filenames to Upper or Lower Case (in bulk) in PHP

  1. This is a message to the admin. Your website is missing out on at least 300 visitors per day. I have found a company which offers to dramatically increase your traffic to your website: http://voxseo.com/traffic/ They offer 1,000 free visitors during their free trial period and I managed to get over 30,000 visitors per month using their services, you could also get lot more targeted traffic than you have now. Hope this helps 🙂 Take care.

  2. This was VERY helpful….thanks so much for posting it. Can you advise where I would put the “.jpg” to not change the extension to uppercase?

  3. Washing the blunder can also be very simple, you must clean and pileup the chips and jus crease the stand to its initial place.

Leave a comment