【问题标题】:Combining multiple php files合并多个php文件
【发布时间】:2014-08-21 18:33:22
【问题描述】:

上一个问题提供的当前代码正在运行。我需要帮助来修改它,以便我可以告诉它要组合哪些 php 文件。

下面的代码结合了它在一个目录中找到的每个 .php 文件。

(即:只需要合并以下页面,page1.php, page2.php, page3.php, page4.php, page5.php, page6.php, page7.php, page8.php, page9.php, page10.php, page11.php, page12.php)

提前致谢。

<?php

foreach (glob("*.php") as $filename) {
  $code.=file_get_contents("./$filename");
}
file_put_contents("./combined.php",$code); 

?>

【问题讨论】:

    标签: php


    【解决方案1】:

    如果您知道文件名并且它们不会更改,那么您可以这样做:

    <?php
    $files = array("a.php", "b.php", "c.php");
    $comb;
    foreach ($files as $k)
    {
        $comb .= file_get_contents("./".$k);
    }
    file_put_contents("./combined.php",$comb);
    ?>
    

    如果您是从表单提交中获取它们,请执行以下操作:

    <?php
    $files = array();
    foreach ($_POST['files_to_combine'] as $k)
    {
        $files .= $k;
    }
    $comb;
    foreach ($files as $k)
    {
        $comb .= file_get_contents("./".$k);
    }
    file_put_contents("./combined.php",$comb);
    ?>
    

    ** 作为安全说明,如果您使用第二种方法,请确保清理您的输入!此代码只是一个概念证明,以使其易于理解和使用。

    【讨论】:

    • 谢谢。是的,数据是纯文本或下拉选项。我每周都会为一家管理公司进行几天的租金检查。老板说如果我能让它像现在这样工作,他会让我使用它。现在它都在纸上,有时35页或更多。然后当我晚上回家时,我必须将它输入到电子表格中,其中包含我拍摄的 100 多张照片并进行分类,然后打印为 PDF。两晚都花在这上面的额外三四个小时,我没有得到报酬。所以如果我能让这个工作我会很高兴,这也会让妻子很高兴。
    【解决方案2】:

    我不知道为什么我对要求修改它的问题持否定态度,看看回答它的程序员是如何从另一个网站复制它的。

    我不是以此为生,也没有计划。这些是个人的 我尝试做的事情是让我的工作更轻松,而且不用纸。

    这就是答案。

     <?php
    
     $txt1 = file_get_contents('page-001.php');
     $txt1 .= "\n" . file_get_contents('page-002.php');
     $txt1 .= "\n" . file_get_contents('page-003.php');
    
     $txt1 .= "\n" . file_get_contents('page-004.php');
     $txt1 .= "\n" . file_get_contents('page-005.php');
     $txt1 .= "\n" . file_get_contents('page-006.php');
    
     $txt1 .= "\n" . file_get_contents('page-007.php');
     $txt1 .= "\n" . file_get_contents('page-008.php');
     $txt1 .= "\n" . file_get_contents('page-009.php');
    
     $txt1 .= "\n" . file_get_contents('page-010.php');
     $txt1 .= "\n" . file_get_contents('page-011.php');
     $txt1 .= "\n" . file_get_contents('page-012.php');
    
     $fp = fopen('newcombined.php', 'w');
     if(!$fp)
        die('Could not create / open text file for writing.');
        if(fwrite($fp, $txt1) === false)
          die('Could not write to text file.');
    
          echo 'Text files have been merged.';    
    ?> 
    

    【讨论】:

    • 只是一个指针,U 可以使用 PHP_EOL 而不是 "\n" 用于那些换行符
    • 谢谢。我会改变它。如果没有引号,那会是我们的吗
    • 是的,它是一个 php 常量。使用 PHP_EOL 的优点是它独立于平台工作
    猜你喜欢
    • 2013-02-11
    • 2018-07-04
    • 1970-01-01
    • 2013-07-19
    • 2012-05-16
    • 2014-12-23
    • 2011-03-15
    • 2010-12-29
    相关资源
    最近更新 更多