この記事には広告を含む場合があります。
記事内で紹介する商品を購入することで、当サイトに売り上げの一部が還元されることがあります。
ディレクトリ内にあるファイル一覧の取得
ワイルドカード(*)が利用できるglob関数をして、ディレクトリ配下にあるファイル一覧を取得します。
使い方
「sample」フォルダ内にあるファイル一覧を取得する方法記載します。
foreach(glob('sample/*') as $file){
if(is_file($file)) {
echo $file;
}
}
サンプルプログラム
「使い方」よりも実用的な関数化したサンプルプログラムを記載します。
sample
┣ sample.txt
┣ test1.php
┗ test2.php
のファイル構成したフォルダをサンプルとして用意しています。
サンプルプログラムの出力結果
[root@hostname]$ php analysis.php array(3) { [0]=> string(19) "./sample/sample.txt" [1]=> string(18) "./sample/test1.php" [2]=> string(18) "./sample/test2.php" } array(2) { [0]=> string(18) "./sample/test1.php" [1]=> string(18) "./sample/test2.php" } array(1) { [0]=> string(19) "./sample/sample.txt" }
ウェブプログラミングについては下記の本も参考になるので、スキルアップにお役立てください。


