- Products -


Software


NEWS


Special



- Knowledge -


Category × Tag



- Like -

公開
作成日:2021/10/9
更新日:2022/3/4

【PHP】フォルダ内のすべてのファイルを取得しリンクを自動生成する方法、サブフォルダ内まで探索

・特定のフォルダ配下のファイル一覧とリンクを生成する例


function getFileList($dir, $filter="") {
    $files = glob(rtrim($dir, '/') . '/*');
    $list = array();
    foreach ($files as $file) {
        if (is_file($file)) {
            if($filter !== ""){
                if(str_contains($file, $filter))
                {
                    $list[] = $file;
                }
            }else{
                $list[] = $file;
            }
        }
        if (is_dir($file)) {
            $list = array_merge($list, getFileList($file, $filter));
        }
    }
    return $list;
}

// 対象のルートフォルダを指定
$result = getFileList("sample_vue", ".html");

$html = "<ul>";
foreach ($result as &$value) {
    $html .= ("<li><a href='" . $value . "'>" . $value . "</a></li>");
}
$html .= "</ul>";

echo $html;


参考


https://blog.asial.co.jp/1250


Category



Tag




関連記事


{{tmp.name}}

{{article.category}} {{article.title}}