A recursive remove directory function for PHP?

前端 未结 7 990
眼角桃花
眼角桃花 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:22

    I prefer an enhaced method derived from the php help pages http://php.net/manual/en/function.rmdir.php#115598

     // check accidential empty, root or relative pathes
     if (!empty($path) && ...)
     {
      if (PHP_OS === 'Windows')
      {
        exec('rd /s /q "'.$path.'"');
      }
      else
      {
          exec('rm -rf "'.$path.'"');
      }
    }
    else
    {
        error_log('path not valid:$path'.var_export($path, true));
    }
    

    reasons for my decision:

    • less code
    • speed
    • keep it simple
    0 讨论(0)
  • 2020-11-30 02:29

    There is another thread with more examples here: How do I recursively delete a directory and its entire contents (files + sub dirs) in PHP?

    If you are using Yii then you can leave it to the framework:

    CFileHelper::removeDirectory($my_directory);
    
    0 讨论(0)
  • 2020-11-30 02:34

    What about this?

    foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirPath, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $path) {
            $path->isDir() && !$path->isLink() ? rmdir($path->getPathname()) : unlink($path->getPathname());
    }
    rmdir($dirPath);
    
    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2020-11-30 02:39

    If the server of application runs linux, just use the shell_exec() function, and provide it the rm -R command, like this:

        $realPath = realpath($dir_path);
    
        if($realPath === FALSE){
             throw new \Exception('Directory does not exist');
        }
    
        shell_exec("rm ". escapeshellarg($realPath) ." -R");
    

    Explanation:

    Removes the specified directory recursively only if the path exists and escapes the path so that it can only be used as a shell argument to avoid shell command injection.

    If you wouldnt use escapeshellarg one could execute commands by naming the directory to be removed after a command.

    0 讨论(0)
  • 2020-11-30 02:40
    public static function rrmdir($dir)
    {
        if (is_dir($dir)) {
            $files = scandir($dir);
            foreach ($files as $file) {
                if ($file != "." && $file != "..") {
                    if (filetype($dir . "/" . $file) == "dir")
                        self::rrmdir($dir . "/" . $file);
                    else
                        unlink($dir . "/" . $file);
                }
            }
            reset($files);
            rmdir($dir);
        }
    }
    
    0 讨论(0)
提交回复
热议问题