ディレクトリに設置してあるフォルダを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のZipArchive::addFileでファイルが追加できない場合の対応方法
過去記事を参考にPHPにて複数のファイルをzipファイル化後、ダウンロードさせた ...
-
-
PHPで「ramsey/uuid」ライブラリを用いてUUIDを生成する方法
PHPでUUID(Universally Unique Identifier)を ...
-
-
PHPのsetcookieで「Cannot modify」エラーの対応方法
PHPでsetcookieを使うと「Warning: Cannot modify ...
-
-
PHPでファイルアップロード時にディレクトリトラバーサル攻撃の対策をする方法
あるシステムのセキュリティ対策としてディレクトリトラバーサル(Directory ...
-
-
file_get_contentsを使用する際にタイムアウト設定
20秒に1回自動でリロードするページにて file_get_contentsを使 ...