Posted By

jatkins on 07/19/08


file files extension extensions

Versions (?)

Who likes this?

4 people have marked this snippet as a favorite


PHP File extension case changer

 / Published in: PHP

Small script to rename files in a given directory so that their extension is lowercase (i.e. example.JPEG becomes example.jpeg). Useful for keeping extensions consistent before uploading to a server with a case sensitive file system.

Note this includes code specifically for PHP on Windows (merely because it was written for use on my local XP machine) which remove the XP-created "Thumbs.db" files. On OS X, the equivalent is .DS_STORE. I don't now about Linux (it probably differs depending on the distribution??).

  1. <?php
  2. $root = $_GET['root'];
  3. $files = array_merge(glob("$root*.*"),glob("$root*\*.*"),glob("$root*\*\*.*"));
  4. $z=0;
  5. for($i=0;$i<count($files);$i++) {
  6. if(stripos($files[$i],"Thumbs.db")) {
  7. unlink($files[$i]);
  8. $z++;
  9. }
  10. else {
  11. rename($files[$i],strtolower($files[$i]));
  12. }
  13. }
  14. echo "$i files renamed. $z Thumbs.db files deleted.";
  15. ?>

Report this snippet  


RSS Icon Subscribe to comments
Posted By: el-artiste on September 16, 2009

hi there... really cool code, it could still use some tweaks though. but this has been a really big help.

check out my mod of the code ""

Posted By: jatkins on July 15, 2010

glad it helped, your code looks useful as it can be adjusted for any extension.

You need to login to post a comment.