勉強したことのメモ

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で「Unable to allocate~」とエラー

PHPで「Unable to allocate memory for pool」 ...

PHPでURLを解析してクエリ(GETパラメータ)を抽出する方法

formからGETで送信したクエリ(パラメータ)を、受信した側で抽出し、キーと値 ...

Cookieをできるだけ長い期間保存する方法

あるシステムの中でCookieを可能な限り長い期間保存したいというリクエストを受 ...

PHPMailerを使ってメールをSMTP送信する方法(Composer無し)

PHPでメールをSMTP送信したかった。また、レンタルサーバだったのでCompo ...

PHPで配列じゃないものに対してソート

PHP Warning:  sort() expects parameter 1 ...