勉強したことのメモ

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のsetcookieで「Cannot modify」エラーの対応方法

PHPでsetcookieを使うと「Warning: Cannot modify ...

CodeIgniter4で簡易版ログインシステムの実装方法(管理画面向け)

CodeIgniter4で管理画面向けの簡易版ログインシステムを作成したい。通常 ...

Codeigniter4でMySQLに接続しCRUD操作する方法

Codeigniter4.4.4でMySQLに接続しCRUD操作したい。また、任 ...

PHPで「Call to undefined function mb_str_split()」エラーの対応方法

PHPにて「Fatal error: Uncaught Error: Call ...

PHPで変数名を動的に変えて使う方法

変数名を動的に変えて使用したかった。 調べてみると可変変数というものがあり、希望 ...