ディレクトリに設置してあるフォルダを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でCSVファイルを出力する際に「①」や「㈱」等の環境依存文字が文字化けする対応方法
過去記事の方法を用いてPHPでCSVファイルを出力すると所々「?」という形に文字 ...
-
-
PHPで日時比較時の注意
PHPで日時を比較する際にミスがあったのでメモ。 基本形は下記。 $limit_ ...
-
-
PHPでCookie保存時にHttpOnly / Secure属性を設定する方法
あるシステムのセキュリティ対策としてCookie保存時にHttpOnly / S ...
-
-
PHPでCookie保存時にPartitioned属性を設定する方法
あるページをブラウザの開発者ツールで見た際に「cookie partitione ...
-
-
PHPで「operator not supported for strings」エラーの対応方法
PHPにて「Fatal error: [] operator not suppo ...