function destroyDir($dir) { if (!is_dir($dir) || is_link($dir)) { return unlink($dir); } foreach (scandir($dir) as $file) { if ($file == '.' || $file == '..') { continue; } if (!destroyDir($dir . DIRECTORY_SEPARATOR . $file)) { chmod($dir . DIRECTORY_SEPARATOR . $file, 0777); if (!destroyDir($dir . DIRECTORY_SEPARATOR . $file)) { return false; } } } return rmdir($dir); }
/** * Destroys or deletes a directory * @param <string> $dir Directory to be destroyed * @param <boolean> $virtual (optional) TRUE if it is a virtual directory * @return <boolean> TRUE if the directory is destroyed successfully * @category Files * <code> * $result = Files::destroyDirectory('C:/test'); * </code> */ function destroyDirectory($dir, $virtual = FALSE) { try { $ds = DIRECTORY_SEPARATOR; $dir = $virtual ? realpath($dir) : $dir; $dir = substr($dir, -1) == $ds ? substr($dir, 0, -1) : $dir; if (is_dir($dir) && ($handle = opendir($dir))) { while ($file = readdir($handle)) { if ($file == '.' || $file == '..') { continue; } elseif (is_dir($dir . $ds . $file)) { destroyDir($dir . $ds . $file); } else { unlink($dir . $ds . $file); } } closedir($handle); rmdir($dir); return TRUE; } else { return FALSE; } } catch (Exception $err) { return $err->getMessage(); } }
/** * Destroy a dir * * @param string $dir * @param bool $virtual */ public static function destroyDir($dir, $virtual = false) { $ds = DIRECTORY_SEPARATOR; $dir = $virtual ? realpath($dir) : $dir; $dir = substr($dir, -1) == $ds ? substr($dir, 0, -1) : $dir; if (is_dir($dir) && $handle = opendir($dir)) { while ($file = readdir($handle)) { if ($file == '.' || $file == '..') { continue; } elseif (is_dir($dir.$ds.$file)) { destroyDir($dir.$ds.$file); } else { unlink($dir.$ds.$file); } } closedir($handle); rmdir($dir); return true; } else { return false; } }
private function downloadAsZip() { try { $finalSrc = $this->templatesDir . '/' . $this->templateName . '/'; $zipName = $this->templateName . "-" . time() . ".zip"; $tmpDest = $this->workingDirectory . $zipName; App_File_Zip::CreateFromFilesystem($finalSrc, $tmpDest); // Destroy the final src destroyDir($finalSrc); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($zipName) . '"'); header('Content-Length: ' . filesize($tmpDest)); readfile($tmpDest); // Remove the unpacks directory destroyDir($this->workingDirectory . $this->templateName); // Remove zip from the tmp dir unlink($tmpDest); } catch (App_File_Zip_Exception $e) { // Zip file was not created. destroyDir($finalSrc); } }