rootpath = $rootpath; if (is_dir($this->rootpath)) { $this->rootpath = pathinfo($this->rootpath, pathinfo_dirname) . directory_separator . pathinfo($this->rootpath, pathinfo_basename); $this->opdirectory = dir($this->rootpath); } else { $this->errormsg = '您提供的目录不存在!'; $this->errorno = 1001; throw new exception($this->errormsg, $this->errorno); } } private function read($directory, $parentpath, $modeinfo = 'mixed', $defaultdir = false, $fullpath = false) { $dirinfo = array(); while (false !== ($childdirorfilename = $directory->read())) { switch ($modeinfo) { case self::recdir_mixed: if ($defaultdir) { $dirinfo[] = $fullpath ? $parentpath . directory_separator . $childdirorfilename : $childdirorfilename; } else { if ($childdirorfilename != '.' && $childdirorfilename != '..') { $dirinfo[] = $fullpath ? $parentpath . directory_separator . $childdirorfilename : $childdirorfilename; } } break; case self::recdir_dir: if (is_dir($parentpath . directory_separator . $childdirorfilename)) { if ($defaultdir) { $dirinfo[] = $fullpath ? $parentpath . directory_separator . $childdirorfilename : $childdirorfilename; } else { if ($childdirorfilename != '.' && $childdirorfilename != '..') { $dirinfo[] = $fullpath ? $parentpath . directory_separator . $childdirorfilename : $childdirorfilename; } } } break; case self::recdir_file: if (is_file($parentpath . directory_separator . $childdirorfilename)) { $dirinfo[] = $fullpath ? $parentpath . directory_separator . $childdirorfilename : $childdirorfilename; } break; } } return $dirinfo; } /** * (php 5 >= 5.4.0)
* 得到目录下的直接子目录或直接子文件信息 * @param string $modeinfo[可选] * 返回目录下信息的模式 * mixed 返回所有的文件名及目录名 * dir 返回所有的目录名 * file 返回所有的文件名 *
* @param bool $defaultdir[可选] * 是否包括默认的链接目录..和. * false 不包括 * true 包括 *
* @param bool $fullpath[可选] * 是否返回子文件或目录的路径信息 * true 是 * false 否 * * @return array 返回一个数组,记录了该目录下的信息 */ public function getpathdirectdirinfo($modeinfo = 'mixed', $defaultdir = false, $fullpath = false) { return $this->read($this->opdirectory, $this->rootpath, $modeinfo, $defaultdir, $fullpath); } }//----------------------------test-----------------------------------------header(content-type:text/html; charset=utf-8);try { $recdir = new recdir('./calltemp/'); $dirs = $recdir->getpathdirectdirinfo('file', true, true); var_dump($dirs);} catch (exception $ex) { echo '在文件【' . $ex->getfile() . '】中的第' . $ex->getline() . '行报错:' . $ex->getmessage() . '(' . $ex->getcode() . ')';}