A recursive remove directory function for PHP?

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

提交回复
热议问题