ディレクトリに設置してあるフォルダを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で配列じゃないものに対してソート
PHP Warning: sort() expects parameter 1 ...
-
-
PHP8系からは文字列検索はstrposよりstr_containsが良さそう
PHP8.0のアップデートに関する記事を読んでいたところstr_contains ...
-
-
PHPとGoogle Authenticatorの組み合わせで2段階認証を実装する方法
2段階認証と言えばSMS送信のケースが多く、その次に通常のメール送信というケース ...
-
-
PHPで作成したプログラムをcron設定し自動実行する方法
cronの設定方法。サーバー側での設定とPHP側での書き方を以下にメモ。 &nb ...
-
-
HTMLにてaタグクリック時にping属性のデータをPHPで受け取る方法
HTMLのaタグにping属性というものを設定できるらしく、設定したリンクをクリ ...