ディレクトリに設置してあるフォルダをPHPで調べて表示
2024/04/18
やりたい事は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
関連記事
-
-
PHPにてユニークかつランダム英数字の文字列を作成する方法
PHPでユニークな文字列を作成したかった。MySQLとかのオートインクリメントの ...
-
-
PHP8系で「Uncaught TypeError: Unsupported operand types」エラー対応方法
PHP8系で「Fatal error: Uncaught TypeError: ...
-
-
PHPMailerを使ってメールをSMTP送信する方法(Composer無し)
PHPでメールをSMTP送信したかった。また、レンタルサーバだったのでCompo ...
-
-
PHPからWebAPI(screendot)経由で他サイトのスクリーンショットを取得する方法
PHPで他サイトのスクリーンショットを取得したい。今回はライブラリは無しで実現し ...
-
-
PHPでプロキシ経由で他サーバーに接続する方法(file_get_contents / curlの2パターン)
PHPでfile_get_contentsやcurlで他サーバに接続する際に自サ ...