ディレクトリに設置してあるフォルダを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
関連記事
-
-
PHPで配列を確認する際にvar_dumpよりも見やすくなるdBug.phpの利用方法
PHPで配列を確認する際、大抵var_dumpで出力させているがこれが見づらい。 ...
-
-
PHPでOGPタグ(metaタグ)を簡単に取得できる「OpenGraph.php」の利用方法
PHPでOGPタグ(metaタグ)を取得する際、file_get_content ...
-
-
PHPにて「ImageHash」ライブラリを使用し画像の類似度を算出する方法
以前にPHPの「image-comparator」ライブラリで画像の類似度を算出 ...
-
-
imgタグのsrc属性のURLにPHPでタイムスタンプを付与しキャッシュ対策する方法
imgタグで画像を表示する際、画像を書き換えても同じものが表示されるというケース ...
-
-
PHPで「Call to undefined function mb_str_split()」エラーの対応方法
PHPにて「Fatal error: Uncaught Error: Call ...