勉強したことのメモ

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

  関連記事

PHPMailerでエラーメッセージ及びデバッグログの表示方法について
PHPMailerでエラーメッセージ及びデバッグログの表示方法について

PHPMailerでエラーメッセージ及びデバッグログを表示させたいというケースが ...

PHPでルーティング用ライブラリ「AltoRouter」の利用方法
PHPでルーティング用ライブラリ「AltoRouter」の利用方法

PHPフレームワークのCodeIgniterを勉強していた際にルーティング機能が ...

PHPでCannot re-assign auto……のエラー
PHPでCannot re-assign auto……のエラー

PHPバージョンアップに伴い「Fatal error: Cannot re-as ...

GoogleアナリティクスのデータをPHPで取得する方法
GoogleアナリティクスのデータをPHPで取得する方法

Googleアナリティクスの特定データをPHPで取得して、当該データを表示なりC ...

Class 'DOMDocument' not foundエラーの対処方法
Class 'DOMDocument' not foundエラーの対処方法

PHPでphpQueryというライブラリを利用しようとすると「Fatal err ...