ディレクトリに設置してあるフォルダを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
関連記事
-
-
htmlspecialcharsを通してMySQLに格納したものを元に戻して出力
MySQLにHTMLタグを入れたくない場合にhtmlspecialcharsを使 ...
-
-
PHPからDBX Platformを利用してDropbox内にディレクトリ(フォルダ)を作成する方法
PHPからDBX Platformを利用してDropbox内にディレクトリ(フォ ...
-
-
PHPで「Call to undefined function mb_str_split()」エラーの対応方法
PHPにて「Fatal error: Uncaught Error: Call ...
-
-
セレクトメニューとテキストエリアの連動
やりたかった事は、 ・セレクトメニューで何れかを選択すると連動したテキストエリア ...
-
-
PHPのsetcookieで「Cannot modify」エラーの対応方法
PHPでsetcookieを使うと「Warning: Cannot modify ...