ディレクトリに設置してあるフォルダをPHPで調べて表示
2024/04/18
やりたい事はsampleというディレクトリがあったとして、その直下にあるフォルダを全部調べて出力させたいというもの。以下に対応方法をメモ。
ソースコード
<?php
if ($dir = opendir("/hogehoge/hugahuga/sample/")) { //ディレクトリ開く
while (($file = readdir($dir)) !== false) { //ディレクトリから名前出してく
if ($file != '.htaccess') { //読み込んで欲しくないファイルはここではじく
echo "<a href='https://test.com/sample/$file/' target='_blank'>$file</a>\n<br />";
}
}
closedir($dir);
}
?>
リファレンス
opendir
https://www.php.net/manual/ja/function.opendir.php
readdir
https://www.php.net/manual/ja/function.readdir.php
closedir
関連記事
-
-
Opauthで「Please change the…」エラーの対応方法
Opauthを使用中に「Notice: Please change the va ...
-
-
PHPで「operator not supported for strings」エラーの対応方法
PHPにて「Fatal error: [] operator not suppo ...
-
-
PHPにて「hashids(Sqids)」ライブラリで数値からユニークなランダム英数字のIDに変換する方法
PHPにてユーザID等を推測されないよう数値のIDからユニークなランダム英数字の ...
-
-
MySQLで使用するインデックスを指定する
ちゃんと指定してるのにインデックスが使用されていない場合、 オプティマイザに知ら ...
-
-
ファイルの更新日付取得とリネーム
やりたかった事は、画像ファイルの更新日時取得と リネーム。 ■参考サイト htt ...