ディレクトリに設置してあるフォルダを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);
}
?>
<?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);
}
?>
<?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の日付・時刻関連
使えそうなものをまとめてメモ。 【関数】 ----------- ■date 日 ...
-
-
PHPでLINEログイン後「ユーザー名」「ユーザーID」「プロフィール画像URL」「メッセージ」を取得する方法
<a>タグのリンクからページ遷移するとLINEログインし、その後に「 ...
-
-
PHPでメルマガを配信する方法(BCCで一括送信するパターン)
PHPでメールマガジンを配信したかった。宛先のメールアドレスはMySQLに保存さ ...
-
-
PHPでeval実行時に「eval()'d code on line」エラー
PHPでeval実行時に「eval()'d code on line 〇〇」とい ...
-
-
PHPにて三項演算子をより簡単に書けるNull合体演算子について
PHPにて〇〇という変数もしくは配列に値があれば××という変数にその値を格納、無 ...