当前位置: 动力学知识库 > 问答 > 编程问答 >

recursion - check path to see if there is any directory php

问题描述:

How to check if in directory "713" there is any directory (not files)? it has to be smart enough to ignore file existance

 ...

...

$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;

网友答案:
    ...
    ...

    $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;

The other solution will be counting slashes "/"

  $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;
分享给朋友:
您可能感兴趣的文章:
随机阅读: