doushang2571 2013-09-06 22:35
浏览 17
已采纳

通过单个PHP脚本运行数百个文件[关闭]

I need to run 600 XML files through a script I've made that extracts specific pieces of information and saves each one in JSON format. All 600 XML files are inside a folder ready to be run through the PHP file, I'm now looking for a fast way to do it.

Essentially this is the process the PHP file goes through:

PHP reads single XML file via URL -> locally saves important info in variables -> saves important info into JSON file

Is there a way I can somehow run all 600 XML files through my PHP file?

Thanks

  • 写回答

4条回答 默认 最新

  • drtkyykai004574380 2013-09-06 22:52
    关注

    Open the directory containing the XML files and then process them, here are some of the most common way todo that.

    opendir()

    <?php
    $dir = "/etc/php5/";
    
    // Open a known directory, and proceed to read its contents
    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
            while (($file = readdir($dh)) !== false) {
                echo "filename: $file : filetype: " . filetype($dir . $file) . "
    ";
            }
            closedir($dh);
        }
    }
    ?>
    

    You can also use glob()

    <?php
    foreach (glob("*.txt") as $filename) {
        echo "$filename size " . filesize($filename) . "
    ";
    }
    ?>
    

    Inside the foreach loop of whichever you choose you can use file_get_contents() or fread() then you can do your conversion to json.

    <?php
    // get contents of a file into a string
    $filename = "/usr/local/something.txt";
    $handle = fopen($filename, "r");
    $contents = fread($handle, filesize($filename));
    fclose($handle);
    ?>
    

    Hope it helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入