ディレクトリに設置してあるフォルダを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のswitch文で「>」「<」等、比較演算子の使用について
switch文は指定の変数が、特定の値もしくは文字列だった場合のみに使うようなも ...
-
-
htaccessとhttpd.confの優先度
既に完成しているシステムを引き継ぐことがあり、そこのリライトルール設定を変更した ...
-
-
PHPで「Unable to allocate~」とエラー
PHPで「Unable to allocate memory for pool」 ...
-
-
ServersManのVPSにRuby on Railsを導入する方法
Ruby on Railsを導入した際のメモ。Cens OSのバージョンは6.3 ...
-
-
日付の比較で○日前というのを調べる
PHP及びjavascriptで日付の比較をしたく、締切日とかではなく ○日前と ...