DirectoryIterator:PHPの超便利ファイル・ディレクトリライブラリ

PHP, SPL

08:40:04, by admin Email , 76 words, 4317 views   Japanese (JP)

del.icio.us
ディレクトリ・ファイル構造を一度に取得出来る。

(ノ・・)ン。。。。。。(((●コロコロッ

SPLなのでPHP5以上

<?php
$path = ".";
try{
$dirIterator = new RecursiveDirectoryIterator($path);
foreach ( $iterator = new RecursiveIteratorIterator($dirIterator
, RecursiveIteratorIterator::SELF_FIRST) as $file){
echo str_repeat(" ", $iterator->getDepth());
if( $file->isDir() ) // is directory
echo $file->getFilename()."/\n";
else // is file
echo $file->getFilename()."\n";
}

// ReflectionObject::export( $iterator );
// ReflectionClass::export('RecursiveIteratorIterator');

}catch( Exception $errorObj ){
echo $errorObj->getMessage();
}


これで配下のディレクトリを一気に処理出来る。

[More:]


dokuwikiの索引機能が気に入らなかったので、
自前で作ることにした。
dokuwikiは仕組み上ディレクトリとファイル構造がそのままWikiの構造になっているので、
ディレクトリとファイルを処理する仕組みが必要になる。

しかし、PHP標準でついているopendir関数はあまりにも使いにくく、
一目見てやる気がなくなった。
もう、配列とかハッシュとかやめて欲しい。



もうちょっと簡単にいかないものかと模索すると良いのがあった。
それが、

この辺にまとめた。
DirectoryIterator ファイル・ディレクトリ制御クラスライブラリ
RecursiveDirectoryIterator 再帰型ファイル・ディレクトリ制御


RecursiveDirectoryIteratorは非常に便利だが、
たぶんDOMと一緒で、
内容が多い場合は重くなる可能性がある。
なので、内容をキャッシュする。
あるいは、DirectoryIterator、RecursiveDirectoryIteratorの利用の切り分けをキチンとするのがよい。



参考:
http://www.webcheatsheet.com/php/working_with_directories.php

Trackback address for this post:

http://hain.jp/htsrv/trackback.php/218

Comments, Trackbacks:

No Comments/Trackbacks for this post yet...

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))
This is a captcha-picture. It is used to prevent mass-access by robots.

Please enter the characters from the image above. (case insensitive)

powered by b2evolution

shinobi

Neighbors
Relative
Favorites

極論istの技術屋を始めて早幾年。 流れの速い業界の波にもまれながらも精一杯生きている様をとくとごらんあれ。

Archives
スポンサー

Latest bookmark
Search

Categories

Who's Online?
Misc
Syndicate this blog XML

Valid XHTML 1.0! Valid CSS! Valid RSS 2.0! Valid Atom 1.0!