--- imagecache_linux.module	2008-09-11 16:41:46.000000000 +0200
+++ imagecache_windows.module	2008-09-11 16:41:46.000000000 +0200
@@ -436,11 +436,20 @@ function imagecache_build_derivative($ac
   // get the folder for the final location of this preset...
   $dir = dirname($dst);
 
-  // Build the destination folder tree if it doesn't already exists.
-  if (!file_check_directory($dir, 1) && !mkdir($dir, 0775, true)) {
-    watchdog('imagecache', t('Failed to create imagecache directory: %dir', array('%dir' => $dir)), WATCHDOG_ERROR);
-    return false;
-  }
+    // Build the destination folder tree if it doesn't already exists.
+    if (!file_check_directory(file_create_path($dst))) {
+      $folders = explode("/", _imagecache_strip_file_directory(dirname($dst)));
+  
+      foreach ($folders as $folder) {
+        $tpath[] = $folder;
+        $checkpath = implode("/", $tpath);
+        if (!file_check_directory(file_create_path($checkpath), FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
+          watchdog('imagecache', t('Cannot create/fix dir(%d).', array('%d' => $checkpath)), WATCHDOG_ERROR);
+          header("HTTP/1.0 500 Internal Server Error");
+          exit;
+        }
+      }
+    }
 
   if (!$image = imageapi_image_open($src)) {
     return false; 
