PHP の小さなコード---ディレクトリ内のサブファイルまたはサブディレクトリを読み取る_PHP チュートリアル

WBOY
リリース: 2016-07-13 16:53:55
オリジナル
880 人が閲覧しました

phpの小さなコード----ディレクトリ内のサブファイルまたはサブディレクトリを読み取ります

クラス RecDir {
保護された $rootPath;
保護された $opDirectory;
const RECDIR_MIXED = '混合';
const RECDIR_DIR = 'ディレクトリ';
const RECDIR_FILE = 'ファイル';
public $errorMsg = '';
public $errorNo = 0;
パブリック関数 __construct($rootPath) {
$this->rootPath = $rootPath;
if (is_dir($this->rootPath)) {
$this->rootPath = pathinfo($this->rootPath, DIRECTORY_SEPARATOR) .
$this->opDirectory = dir($this->rootPath);
} 他 {
$this->errorMsg = '指定したディレクトリは存在しません。 ';
$this->errorNo = 1001;
throw new Exception($this->errorMsg, $this->errorNo);
}
}
プライベート関数 read($directory, $parentPath, $modeInfo = 'mixed', $defaultDir = false, $fullPath = false) {
$dirInfo = array();
while (FALSE !== ($childDirOrFileName = $directory->read())) {
スイッチ ($modeInfo) {
ケース self::RECDIR_MIXED:
$dirInfo[] = $parentPath : $childDirOrFileName ?
} else {
H If ($ ChilddirorFilename! = '.' && $ ChilddirorFilename! = '..') {
$dirInfo[] = $fullPath ?
}
休憩;
ケース self::RECDIR_DIR:
if (is_dir($parentPath . DIRECTORY_SEPARATOR . $childDirOrFileName)) {
$dirInfo[] = $fullPath ?
if($ childdirorfilename!= '。' && $ childdirorfilename!= '..'){
$dirInfo[] = $fullPath ? Y_SEPARATOR . $childDirOrFileName : $childDirOrFileName;
}
休憩;
ケース self::RECDIR_FILE:
if (is_file($parentPath . DIRECTORY_SEPARATOR . $childDirOrFileName)) {
$dirInfo[] = $fullPath ? $parentPath 。 DIRECTORY_SEPARATOR 。 $childDirOrFileName : $childDirOrFileName;
}
休憩;
}
}
$dirInfo;
を返す
}
/**
* (PHP 5 >= 5.4.0)
* ディレクトリ直下のサブディレクトリまたは直下のサブファイル情報を取得します
* @param string $modeInfo[オプション]

* * ディレクトリ内の情報のモードを返します
* 混合 すべてのファイル名とディレクトリ名を返します
* dir すべてのディレクトリ名を返します
* file すべてのファイル名を返します
* *

* @param bool $defaultDir[オプション]

* デフォルトのリンクディレクトリを含めるかどうか...そして
* falseには
は含まれません
* trueには
が含まれます
* *

* @param bool $fullPath[オプション]

* サブファイルまたはディレクトリのパス情報を返すかどうか
* 本当です
* いいえ
* *

* @return array このディレクトリ内の情報を記録する配列を返します
*/
public function getPathDirectDirInfo($modeInfo = 'mixed', $defaultDir = false, $fullPath = false) {
return $this->read($this->opDirectory, $this->rootPath, $modeInfo, $defaultDir, $fullPath);
}
}
//----------------------------テスト----------------- ------------------------
header("コンテンツタイプ:text/html; charset=UTF-8");
試してください{
$recDir = new RecDir('./CALLTEMP/');
$dirs = $recDir->getPathDirectDirInfo('file', true, true);
var_dump($dirs);
} catch (例外 $ex) {
echo '在文件【' . $ex->getFile() 。 ']中の第' 。 $ex->getLine() 。 「行报错:」 。 $ex->getMessage() 。 '(' . $ex->getCode() . ')';
}

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/1053347.html技術記事 php小代码----目录下读取子文件または子目录 ?php class RecDir { protected $rootPath;保護された $opDirectory; const RECDIR_MIXED = 混合; const RECDIR_DIR = ディレクトリ; const RECD...
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!