勉強したことのメモ

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

  関連記事

Opauthで「Please change the…」エラーの対応方法
Opauthで「Please change the…」エラーの対応方法

Opauthを使用中に「Notice: Please change the va ...

FPDIで「This PDF document probably uses~~」エラーの対処方法
FPDIで「This PDF document probably uses~~」エラーの対処方法

FPIDであるPDFを読み込ませようとすると「This PDF document ...

Fatal error: Cannot redeclare 関数名のエラー
Fatal error: Cannot redeclare 関数名のエラー

PHPにて「Fatal error: Cannot redeclare 【関数名 ...

PHPで月末日を取得
PHPで月末日を取得

PHPファイルを触っている際に日付処理時、 date('t')というものがあって ...

PHPのOpenSSL関数で文字列の暗号・復号化を行う方法
PHPのOpenSSL関数で文字列の暗号・復号化を行う方法

だいぶ前にPHPでBlowfishアルゴリズムを用いた暗号化と復号化する方法をメ ...