勉強したことのメモ

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で定義済みの変数 / 定数とincludeしているファイル一覧を取得する方法

他社が作成されたPHPのファイルを読み解く際に、定義済みの変数 / 定数や、in ...

PHP8系で「Warning: Attempt to read property "xxxxxx" 」エラーの対応方法

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

composerでライブラリのインストールとインストール時のエラー対応方法

composerでライブラリをインストールする際に「The following ...

PhpSpreadsheetでセル内の文字列の改行、セル幅の自動調整を行う方法

以前PHPとPhpSpreadsheetを用いてExcelシートを出力する方法を ...

PHPで「Warning: date(): It is not safe to…」エラーの対応方法

PHPでdate関数を使った際に「Warning: date(): It is ...