勉強したことのメモ

Webエンジニア / プログラマが勉強したことのメモ。

ディレクトリに設置してあるフォルダをPHPで調べて表示

   2024/04/18  PHP

やりたい事は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

https://www.php.net/manual/ja/function.closedir.php

 - PHP

  関連記事

PHPのintval

intvalという見たこと無い関数があったのでメモ。 ■リファレンス http: ...

PHP8系で「Warning: Constant xxxxx already defined in」エラーの対応方法

PHP8系&WordPress6.4.3で「Warning: Const ...

MySQLで直近に挿入したオートインクリメントの値と次回挿入する値を取得する方法

phpとmysqliを使っている中で次回挿入するオートインクリメントの値と、前回 ...

PHP7が2015年11月にリリースされるらしい

PHP7で変わること ——言語仕様とエンジンの改善ポイント from Yoshi ...

PHPで日付の比較

以前に「締切日を設けて、今日の日付と比較して分岐させる」というもので 日付の比較 ...