Index: cacherouter/engines/file.php
===================================================================
--- cacherouter/engines/file.php	(revision 2180)
+++ cacherouter/engines/file.php	(working copy)
@@ -156,13 +156,24 @@
   function key($key) {
     $table = $this->name;
     $fspath = $this->fspath;
+
+    // Make sure we have a good filename when we concatentate $hash with $appendix:
+    // * Can't be over 255 bytes (ext2, 3, 4) or 255 characters (NTFS, FAT) as per 
+    //  http://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits
+    // * Can't include "? * / \ : ; < >" in NTFS and FAT as per 
+    //  http://technet.microsoft.com/en-us/library/cc722482.aspx
     $hash = md5($key);
-    // TODO make sure we always get valid filenames in the appendix
-    $appendix = str_replace(array('/'), array('-'), $key);
+    $appendix = str_replace(array('?','*','/','\\',':',';','<','>'), '-', $key); //Remove those characters that MSFT hates.
+    $filename = $hash . '--' . $appendix;
+    if (function_exists('mb_substr')) {
+      $filename = mb_substr($filename, 0, 255, '8bit');
+    } else {
+      $filename = substr($filename, 0, 255); // We'll have to assume we're working with ASCII if mb_ extension isn't installed.
+    }
     
     $this->create_directory($fspath, $hash{0});
     
-    return  "$fspath/$table/". $hash{0}. '/'. $hash. '--'. $appendix;
+    return "$fspath/$table/" . $hash{0} . '/' . $filename;
   }
   
   /**
