typecho 插件 php读取文件 和 筛选指定文件类型
侧边栏壁纸
  • 累计撰写 13 篇文章
  • 累计收到 7 条评论
    已存活 146231316

typecho 插件 php读取文件 和 筛选指定文件类型

jacksen168
2022-08-19 / 0 评论 / 60 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2023年01月06日,已超过533天没有更新,若内容或图片失效,请留言反馈。

改代码是从我建站第一个项目分离出来的,也是原创

本站第一个项目名为 PL2DForTC
PL2DForTC 全名 PIXI Live2D For Typecho
是按刚刚建站是没有能用的看板娘插件而写出来的

废话不多说直接上代码

function get_json_files($get_josn_files_type,$url,$file_type)
        {
            // 读取XX文件夹
            $models_for_get_json_files = array();
            $models_names = array();
            //读取XX下所有文件夹--重组用
            foreach (glob($url."/*") as $key => $value) {
                $models_for_get_json_files[substr($value, 32)] = substr($value, 32);
                $models_names[$key] = substr($value, 32);
            };
            //读取XX下所有文件夹--检测用
            $models_folders = array();
            foreach (glob($url."/*") as $key1 => $value) {
                $models_folders[$key1] = substr($value, 32);
            };
            $models_folder_files = array();
            //读取 XX文件夹 下 所有 json文件夹下的文件 ,深度 = 1
            for ($i = 0; $i < count($models_folders); $i++) {
                $models_folder = array();
                foreach (glob($url."/" . $models_folders[$i] . "/*") as $c => $value) {
                    $models_folder[$c] = substr($value, strlen($url."/" . $models_folders[$i] . "/"));
                };
                $models_folder_files[$i] = $models_folder; //数组=XX文件夹*n--->单个AA文件夹*n--->文件*n
            };
            $json_files_after_combined_with_folders = array();
            //筛除非json文件--XX文件夹
            for ($f = 0; $f < count($models_folder_files); $f++) { //根据XX文件夹数组长度循环
                $json_files_after_combined_with_folders[$f] = "";
                $L = 0;
                $json_files = array();
                //筛除非json文件
                for ($j = 0; $j < count($models_folder_files[$f]); $j++) {
                    if (substr($models_folder_files[$f][$j], strpos($models_folder_files[$f][$j], '.'.$file_type)) === '.'.$file_type) {
                        //判断方式:判断文件名末尾是否是'.类型'
                        //判断:true
                        $json_files[$L] = $models_folder_files[$f][$j];
                        $L++;
                    };
                };
                $json_files_after_combined_with_folders[$f] = $json_files;
                if ($json_files_after_combined_with_folders[$f] == array()) {
                    $json_files_after_combined_with_folders[$f] = null;
                };
            };

            if ($get_josn_files_type == 0) {
                //model目录下 文件/文件夹的名单 与 模组文件夹下的json文件名单重叠;
                return array_combine($models_names, $json_files_after_combined_with_folders);
                //目录文件可能很多,删除产地的所有变量减少服务器占用内存
                unset($models_names, $models_for_get_json_files, $get_josn_files_type, $json_files_after_combined_with_folders, $json_files, $models_folders, $models_folder_files, $models_folder, $i, $L, $f, $j);
            } else if ($get_josn_files_type == 1) {
                //model目录文件夹名称名单 + 模型文件夹目录文件名称名单
                $get_josn_files_1 = array();
                $get_josn_files_1['name'] = $models_names;
                $get_josn_files_1['files'] = $json_files_after_combined_with_folders;
                return $get_josn_files_1;
                //目录文件可能很多,删除所有产生的变量减少服务器占用内存
                unset($get_josn_files_1);
                unset($models_names, $models_for_get_json_files, $get_josn_files_type, $json_files_after_combined_with_folders, $json_files, $models_folders, $models_folder_files, $models_folder, $i, $L, $f, $j);
            } else if ($get_josn_files_type == 2) {
                $get_josn_files_2_i = 0;
                $get_josn_files_2 = array();
                for ($get_josn_files_2_a = 0; $get_josn_files_2_a < count($models_names); $get_josn_files_2_a++) {
                    if (!$json_files_after_combined_with_folders[$get_josn_files_2_a] == null) {
                        for ($get_josn_files_2_c = 0; $get_josn_files_2_c < count($json_files_after_combined_with_folders[$get_josn_files_2_a]); $get_josn_files_2_c++) {
                            $get_josn_files_2[$get_josn_files_2_i] = $models_names[$get_josn_files_2_a] . '/' . $json_files_after_combined_with_folders[$get_josn_files_2_a][$get_josn_files_2_c];
                            $get_josn_files_2_i++;
                        }
                    }
                }
                return $get_josn_files_2;
                //目录文件可能很多,删除所有产生的变量减少服务器占用内存
                unset($get_josn_files_2_i, $get_josn_files_2, $get_josn_files_2_a, $get_josn_files_2_c);
                unset($models_names, $models_for_get_json_files, $get_josn_files_type, $json_files_after_combined_with_folders, $json_files, $models_folders, $models_folder_files, $models_folder, $i, $L, $f, $j);
            } else if ($get_josn_files_type == 3) {
                $get_josn_files_3 = array();
                for ($get_josn_files_3_i = 0; $get_josn_files_3_i < count($models_names); $get_josn_files_3_i++) {
                    if (!$json_files_after_combined_with_folders[$get_josn_files_3_i] == null) {
                        for ($get_josn_files_3_a = 0; $get_josn_files_3_a < count($json_files_after_combined_with_folders[$get_josn_files_3_i]); $get_josn_files_3_a++) {
                            $get_josn_files_3[$models_names[$get_josn_files_3_i] . '/' . $json_files_after_combined_with_folders[$get_josn_files_3_i][$get_josn_files_3_a]] = $models_names[$get_josn_files_3_i] . '/' . $json_files_after_combined_with_folders[$get_josn_files_3_i][$get_josn_files_3_a];
                        }
                    }
                }
                return $get_josn_files_3;
                //目录文件可能很多,删除所有产生的变量减少服务器占用内存
                unset($get_josn_files_3, $get_josn_files_3_i, $get_josn_files_3_a);
                unset($models_names, $models_for_get_json_files, $get_josn_files_type, $json_files_after_combined_with_folders, $json_files, $models_folders, $models_folder_files, $models_folder, $i, $L, $f, $j);
            }
        };

        $data = get_json_files(1,"../XXX","json");
        // get_json_files(type,url,file_type)
        //type = 你要获取的数据类型
        //url = 你要读取文件的目录,后面不带'/'的,例如:../xxx/xxx/xx
        //file_type = 你要筛选出来的文件类型,如:json/zip/php/html
        //深度为1,例: URL = ../XXX/XXX/  读取范围: ../XXX/XXX/所有文件夹/所有XX.文件

        //输出如下: 

        //------------------------------------type = 0 输出:花名册--------------

        // Array(
        //     [HK_3401] => Array(
        //         [0] => Normal.model3.json
        //         [1] => Normal.physics3.json
        //     )

        //     [HK416_805] => Array ( 
        //         [0] => Normal.model3.json
        //         [1] => Normal.physics3.json
        //     )

        //     [Kalina] => Array (
        //         [0] => Normal.model3.json
        //         [1] => Normal.physics3.json
        //     )

        //     [M4A1_4505] => Array (
        //         [0] => Normal.model3.json
        //     ) 

        //     [Pio] => Array ( 
        //         [0] => Model.json
        //     )

        //     [新建文件夹] = null

        //     [PL2DForTC.md] = null

        //     [和泉纱雾] => Array (
        //         [0] => Normal.model3.json
        //         [1] => Normal.physics3.json
        //     )
        // )

        //--------------------------------typr = 1 输出:文件夹名单和文件夹内json文件名单--------------

        // Array
        // (
        //     [name] => Array
        //         (
        //             [0] => HK416_3401
        //             [1] => HK416_805
        //             [2] => Kalina
        //             [3] => M4A1_4505
        //             [4] => Pio
        //             [5] => 新建文件夹
        //             [6] => PL2DForTC.md
        //             [7] => 和泉纱雾
        //         )

        //     [files] => Array
        //         (
        //             [0] => Array
        //                 (
        //                     [0] => Normal.model3.json
        //                     [1] => Normal.physics3.json
        //                 )

        //             [1] => Array
        //                 (
        //                     [0] => Normal.model3.json
        //                     [1] => Normal.physics3.json
        //                 )

        //             [2] => Array
        //                 (
        //                     [0] => Normal.model3.json
        //                     [1] => Normal.physics3.json
        //                 )

        //             [3] => Array
        //                 (
        //                     [0] => Normal.model3.json
        //                 )

        //             [4] => Array
        //                 (
        //                     [0] => Model.json
        //                 )
        //             [5] => null
        //             [6] => null
        //             [7] => Array
        //                 (
        //                     [0] => Normal.model3.json
        //                     [1] => Normal.physics3.json
        //                 )
        //         )

        // )


        //-------------------------------type = 2 输出:筛选出json文件(URL)--------------------------------

        // Array
        //   (
        //      [0] => HK416_3401/Normal.model3.json
        //      [1] => HK416_3401/Normal.physics3.json
        //      [2] => HK416_805/Normal.model3.json
        //      [3] => HK416_805/Normal.physics3.json
        //      [4] => Kalina/Normal.model3.json
        //      [5] => Kalina/Normal.physics3.json
        //      [6] => M4A1_4505/Normal.model3.json
        //      [7] => Pio/Model.json
        //      [8] => 和泉纱雾/Normal.model3.json
        //      [9] => 和泉纱雾/Normal.physics3.json
        //   )


        //-------------------------------type = 3 输出:筛选出json文件(URL+花名册)--------------------------------

        // Array
        //   (
        //      [HK416_3401/Normal.model3.json] => HK416_3401/Normal.model3.json
        //      [HK416_3401/Normal.physics3.json] => HK416_3401/Normal.physics3.json
        //      [HK416_805/Normal.model3.json] => HK416_805/Normal.model3.json
        //      [HK416_805/Normal.physics3.json] => HK416_805/Normal.physics3.json
        //      [Kalina/Normal.model3.json] => Kalina/Normal.model3.json
        //      [Kalina/Normal.physics3.json] => Kalina/Normal.physics3.json
        //      [M4A1_4505/Normal.model3.json] => M4A1_4505/Normal.model3.json
        //      [Pio/Model.json] => Pio/Model.json
        //      [和泉纱雾/Normal.model3.json] => 和泉纱雾/Normal.model3.json
        //      [和泉纱雾/Normal.physics3.json] => 和泉纱雾/Normal.physics3.json
        //   )
0

评论 (0)

取消