A recursive remove directory function for PHP?

前端 未结 7 1030
眼角桃花
眼角桃花 2020-11-30 01:48

I am using PHP to move the contents of a images subfolder

GalleryName/images/

into another folder. After the move, I need to del

7条回答
  •  借酒劲吻你
    2020-11-30 02:37

    I've adapted a function which handles hidden unix files with the dot prefix and uses glob:

    public static function deleteDir($path) {
        if (!is_dir($path)) {
            throw new InvalidArgumentException("$path is not a directory");
        }
        if (substr($path, strlen($path) - 1, 1) != '/') {
            $path .= '/';
        }
        $dotfiles = glob($path . '.*', GLOB_MARK);
        $files = glob($path . '*', GLOB_MARK);
        $files = array_merge($files, $dotfiles);
        foreach ($files as $file) {
            if (basename($file) == '.' || basename($file) == '..') {
                continue;
            } else if (is_dir($file)) {
                self::deleteDir($file);
            } else {
                unlink($file);
            }
        }
        rmdir($path);
    }
    

提交回复
热议问题