php读取子目录下文件内容,php小代码----目录下读取子文件或子目录_PHP教程

5268f80b9b1e01f982625ef6fac83ca1.png

php小代码----目录下读取子文件或子目录

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() . ')';

}

http://www.bkjia.com/PHPjc/1053347.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1053347.htmlTechArticlephp小代码----目录下读取子文件或子目录 ?php class RecDir { protected $rootPath; protected $opDirectory; const RECDIR_MIXED = mixed; const RECDIR_DIR = dir; const RECD...

1428d0e076c3959ab11d28a39bc84fab.png

5268f80b9b1e01f982625ef6fac83ca1.png

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值