We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

jatkins on 07/19/08


Tagged

file files extension extensions


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

SpinZ
tikitakfire


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 box) 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 

You need to login to post a comment.