检查路径,看看是否有任何目录php

时间:2015-04-14 20:32:35

标签: php recursion directory

如何检查目录“713”中是否有任何目录(不是文件)?它必须足够聪明才能忽略文件存在

    ...
    ...

    $workRecordFullPath = "/var/www/websites/AM_dev/app/webroot/files/submissions/57601/4189/713/";

    // check if folder "713" exists     
    if (file_exists($workRecordFullPath)) {
    // check if into 713 there is any directory
        if (!is_dir($workRecordFullPath)) {
            return true;
        }
    } 
    return false;

1 个答案:

答案 0 :(得分:0)

    ...
    ...

    $workRecordFullPath = "/var/www/websites/AM_dev/app/webroot/files/submissions/57601/4189/713/";
    // get all directories in "713"
    $submissionWorkRecordFullPath = glob($workRecordFullPath . '*' , GLOB_ONLYDIR);

    // check if folder "713" exists     
    if (file_exists($workRecordFullPath)) {
    // check if into 713 there is any directory
        if (!empty($submissionWorkRecordFullPath)) {
            return true;
        }
    } 
    return false;

另一个解决方案是计算斜杠“/”

  $oldQc = 12; // The deep number of folders to wrok_record_id

  $objects = new RecursiveIteratorIterator(
              new RecursiveDirectoryIterator($workRecordFullPath),
              RecursiveIteratorIterator::SELF_FIRST
  );

  foreach($objects as $name => $object){

        // Name of all flies and directories
        $countSlashes = substr_count($name,'/');

        if ($countSlashes>$oldQc) {
            return false;
        }
  }

  return true;