/** * Delete directory content. * * @param string $path * @param string|array $fileFilterRegex [optional] Regex of files to purge (can be negated to skip files) * @param string|array $dirFilterRegex [optional] Regex of dirs to purge (can be negated to skip dirs) */ public static function purge(string $path, $fileFilterRegex = null, $dirFilterRegex = null) { Path::validatePath($path); $iterator = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS); $iterator = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST); foreach ((array) $fileFilterRegex as $regex) { $iterator = new FileRegexIterator($iterator, $regex); } foreach ((array) $dirFilterRegex as $regex) { $iterator = new DirectoryRegexIterator($iterator, $regex); } foreach ($iterator as $file) { if ($file->isDir()) { Path::delete($file->getPathName()); continue; } File::delete($file->getPathname()); } }
<?php use Ansas\Util\Path; // Set script start time isset($_SERVER['REQUEST_TIME_FLOAT']) || ($_SERVER['REQUEST_TIME_FLOAT'] = microtime(true)); // Set default path constants defined('ROOT_PATH') || define('ROOT_PATH', Path::getRoot()); defined('APP_PATH') || define('APP_PATH', ROOT_PATH . '/app'); defined('BIN_PATH') || define('BIN_PATH', ROOT_PATH . '/bin'); defined('ETC_PATH') || define('ETC_PATH', ROOT_PATH . '/etc'); defined('LIB_PATH') || define('LIB_PATH', ROOT_PATH . '/lib'); defined('LOG_PATH') || define('LOG_PATH', ROOT_PATH . '/log'); defined('PUB_PATH') || define('PUB_PATH', ROOT_PATH . '/pub'); defined('SRC_PATH') || define('SRC_PATH', ROOT_PATH . '/src'); defined('TMP_PATH') || define('TMP_PATH', ROOT_PATH . '/tmp'); defined('VAR_PATH') || define('VAR_PATH', ROOT_PATH . '/var'); // Check system environment we are on (production or development system) // Note: If path contains ".develop" we assume we are in development mode define('ENVIRONMENT_PRODUCTION', 'production'); define('ENVIRONMENT_DEVELOP', 'develop'); define('ENVIRONMENT', strpos(__FILE__, '.develop/') === false ? ENVIRONMENT_PRODUCTION : ENVIRONMENT_DEVELOP); define('DEBUG', getenv('DEBUG') || ENVIRONMENT == ENVIRONMENT_DEVELOP); define('SESSION_LIFETIME', 60 * 60 * 24 * 30); define('SESSION_PATH', TMP_PATH . '/session'); define('LOG_FILE', LOG_PATH . '/php.log'); // Set up error handling ini_set('display_errors', ENVIRONMENT == ENVIRONMENT_PRODUCTION ? 'Off' : 'stderr'); ini_set('display_startup_errors', ENVIRONMENT == ENVIRONMENT_PRODUCTION ? 'Off' : 'On'); ini_set('error_reporting', E_ALL); ini_set('error_log', LOG_FILE); ini_set('log_errors', 'On');