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 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败