ディレクトリに設置してあるフォルダを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でルーティング用ライブラリ「AltoRouter」の利用方法
PHPフレームワークのCodeIgniterを勉強していた際にルーティング機能が ...
-
-
PHPで「Unable to allocate~」とエラー
PHPで「Unable to allocate memory for pool」 ...
-
-
PHPからLINEのMessaging APIにリクエストし通知を送る方法
LINEから「2025/3/31にLINE Notifyのサービスが終了する」と ...
-
-
Facebookのタイムラインを取得して表示する方法
Facebookのタイムラインを取得してきて、表示させたいという案件があった。 ...
-
-
Laravel 13系をsystemd timerで自動実行する方法
Laravelで特定の処理をsystemd timerで自動実行したい。また、s ...