勉強したことのメモ

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で外部ファイルから配列を取得

やりたかった事。 ①管理画面で必要項目を入力すると、aaa.phpが 生成される ...

$_SERVER['PHP_SELF']に脆弱性あり

formのaction属性とかページングのリンクとかで、 <form ac ...

PHPで「operator not supported for strings」エラーの対応方法

PHPにて「Fatal error: [] operator not suppo ...

CodeIgniter3でCronを実行する方法

CodeIgniter3系で特定の処理をCronで自動実行したかった。以下に設定 ...

Mailtrap & PHPMailerでメールサーバ無しの環境でもメール送信テストを行う方法

開発環境等メールサーバが無い環境でメール送信テストを行う際にMailtrapとい ...