There are probably about a billion of these things, but I needed one that would manually chmod() if necessary. A little complex of a conditional, but I wanted it condensed.

<?php
  /***************************************************
   * rm_dir(string $path[, string $delim = "/"])     *
   * Deletes the contents of and removes a directory *
   * recursively, chmod()'ing if necessary           *
   *                                                 *
   * @param string $path  Path to the directory      *
   * @param string $delim Path delimiter ("/")       *
   *                                                 *
   * Returns bool                                    *
   *  true if removal is entirely successful         *
   *  false if removal is not entirely successful    *
   ***************************************************/
  function rm_dir($path, $delim = "/")
  {
   $path .= substr($path, -1) != $delim ? $delim : "";
   if(($open = opendir($path)) === false)
    return false;
   while(($fn = readdir($open)) !== false)
    if(($fn != "." && $fn != "..") && ((is_file($path.$fn) && !unlink($path.$fn)) || (is_dir($path.$fn) && !rm_dir($path.$fn))) && (chmod($path.$fn, 0777) && (is_file($path.$fn) && !unlink($path.$fn)) || (is_dir($path.$fn) && !rm_dir($path.$fn))))
     return false;
   closedir($open);
   return rmdir($path);
  }
?>

What do you think?

    You could shorten the code a bit by removing $delim, since you could instead use the predefined constant DIRECTORY_SEPARATOR.

    EDIT: You could further simplify it by simply requiring that "/" be used as the directory separator, since that works on all the O/S's I can think of.

      I can't edit my own post? Guess there's a time limit.

      I totally overlooked the DIRECTORY_SEPARATOR constant. I considered using it now, but you're right, it's unnecessary, since the Windows OS is perfectly OK with forward slashes. 🙂

      <?php
        /***************************************************
         * rm_dir(string $path)                            *
         * Deletes the contents of and removes a directory *
         * recursively, chmod()'ing if necessary           *
         *                                                 *
         * @param string $path  Path to the directory      *
         *                                                 *
         * Returns bool                                    *
         *  true if removal is entirely successful         *
         *  false if removal is not entirely successful    *
         ***************************************************/
        function rm_dir($path)
        {
         $path .= substr($path, -1) != "/" ? "/" : "";
         if(($open = opendir($path)) === false)
          return false;
         while(($fn = readdir($open)) !== false)
          if(($fn != "." && $fn != "..") && ((is_file($path.$fn) && !unlink($path.$fn)) || (is_dir($path.$fn) && !rm_dir($path.$fn))) && (chmod($path.$fn, 0777) && (is_file($path.$fn) && !unlink($path.$fn)) || (is_dir($path.$fn) && !rm_dir($path.$fn))))
           return false;
         closedir($open);
         return rmdir($path);
        }
      ?>
        Write a Reply...