yupe\helpers\YFile::rmDir PHP Method

rmDir() public static method

Рекрусивное удаление директорий.
Since: 0.5
public static rmDir ( $path ) : boolean
$path Если $path оканчивается на *, то удаляется только содержимое директории.
return boolean
    public static function rmDir($path)
    {
        static $doNotRemoveBaseDirectory = false, $baseDirectory;
        $path = trim($path);
        if (substr($path, -1) == '*') {
            $doNotRemoveBaseDirectory = true;
            $path = substr($path, 0, -1);
        }
        if (substr($path, -1) == '/') {
            $path = substr($path, 0, -1);
        }
        if ($doNotRemoveBaseDirectory) {
            $baseDirectory = $path;
        }
        if (is_dir($path)) {
            $dirHandle = opendir($path);
            while (false !== ($file = readdir($dirHandle))) {
                if ($file != '.' && $file != '..') {
                    $tmpPath = $path . '/' . $file;
                    if (is_dir($tmpPath)) {
                        self::rmDir($tmpPath);
                    } else {
                        if (file_exists($tmpPath)) {
                            unlink($tmpPath);
                        }
                    }
                }
            }
            closedir($dirHandle);
            // удаляем текущую папку
            if ($doNotRemoveBaseDirectory === true && $baseDirectory == $path) {
                return true;
            }
            return rmdir($path);
        } elseif (is_file($path) || is_link($path)) {
            return unlink($path);
        } else {
            return false;
        }
    }

Usage Example

Example #1
0
 /**
  * Команда для очистки папки assets.
  *
  * Examples:
  *
  * yiic yupe flushAssets
  *
  * @return bool
  */
 public function actionFlushAssets()
 {
     $dirs = glob(Yii::getPathOfAlias('webroot.assets') . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR);
     foreach ($dirs as $value) {
         if (!\yupe\helpers\YFile::rmDir($value)) {
             $this->log('Failed to remove directory "' . $value . '"', CLogger::LEVEL_ERROR);
         }
     }
     return true;
 }
All Usage Examples Of yupe\helpers\YFile::rmDir