亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

包含文件夾中的所有文件 - PHP

包含文件夾中的所有文件 - PHP

PHP
森林海 2023-08-26 10:19:41
我試圖在 php.ini 中包含另一個文件夾中的一個文件夾中的所有文件。這是我當前的目錄結構:> folder1  main.php> folder2  > folder3  someFile.php  someFile2.php  someFile3.json我嘗試這樣做:include "../folder2/";這(僅包括 php 文件):foreach (glob("classes/*.php") as $filename){    include $filename;}從main.php,我想包含所有內容folder2/,包括子文件夾:folder3以及 中的 php 和 json 文件folder2/。我看過其他堆棧溢出問題并了解 for 循環方法,但還沒有找到包含不同文件類型(.php、.json 等)和子目錄的方法。任何幫助表示贊賞。謝謝!
查看完整描述

1 回答

?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

PHPinclude不應用于其他文件類型,例如.json. 要從這些文件中提取數據,您需要使用類似file_get_contents. 例如:


$data = json_decode(file_get_contents('someFile3.json'));

要遞歸地將 PHP 文件包含在其他目錄中,您可以嘗試遞歸搜索所有目錄:


function require_all($dir, $max_scan_depth, $depth=0) {

    if ($depth > $max_scan_depth) {

        return;

    }


    // require all php files

    $scan = glob("$dir/*");

    foreach ($scan as $path) {

        if (preg_match('/\.php$/', $path)) {

            require_once $path;

        }

        elseif (is_dir($path)) {

            require_all($path, $max_scan_depth, $depth+1);

        }

    }

}


$max_depth = 255;

require_all('folder3', $max_depth);

此代碼是此處代碼的修改版本:https ://gist.github.com/pwenzel/3438784


查看完整回答
反對 回復 2023-08-26
  • 1 回答
  • 0 關注
  • 135 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號