ディレクトリに設置してあるフォルダを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
関連記事
-
-
PHP8系で追加されたJIT(Just-In-Time)の設定方法
PHP8系でOPcacheの拡張機能としてJIT(Just-In-Time)とい ...
-
-
PHP8系からは文字列検索はstrposよりstr_containsが良さそう
PHP8.0のアップデートに関する記事を読んでいたところstr_contains ...
-
-
PHPとajaxでチャットの作成
■ソース ・index.php <? error_reporting(E_ ...
-
-
PHPとLINEを連携させて通知を送る方法(file_get_contents / curlの2パターン)
PHPとLINEを連携させ、LINEに何らかの通知メッセージを送信したい。以下に ...
-
-
PHPでOGPタグ(metaタグ)を簡単に取得できる「OpenGraph.php」の利用方法
PHPでOGPタグ(metaタグ)を取得する際、file_get_content ...