2011-03-06 93 views

回答

2

试试这个:

这行$footer_code添加到所有php文件的末尾$dir

<?php 

    $dir = 'YOUR DIRECTORY'; 
    $footer_code = "footer code"; 

    if ($handle = opendir($dir)) { 
    while (false !== ($file = readdir($handle))) { 
     if (substr($file, -4) == '.php') { 
      $fh = fopen($file, 'a') or die("can't open file"); 
      fwrite($fh, $footer_code); 
      fclose($fh); 
     } 
    } 
    closedir($handle); 
    } 

?> 
+1

substr($ file,-1,4)只返回“p”,你应该使用substr($ file,-4,4)或者substr($ file,-4)来获得.php文件名。 – bhu1st 2011-03-06 08:11:03

+0

@bhu更正。谢谢! – Kyle 2011-03-06 08:13:50

1

有一个Apache模块,可以让你设置一个共同的页脚的每个文件送达,检查此为MROE - >http://freshmeat.net/projects/mod_layout/

+0

谢谢,我将为未来的脚本记住这一点。但是,这不是我想要的特别 – liamzebedee 2011-03-06 08:00:40

+0

如果这不是你想要的,你可以做一些Bash魔术在所有文件中附加一个字符串。 – Kumar 2011-03-06 08:06:46

2

如果这是一些样板代码的所有网页的需要,那么可能我建议使用某种抽象类来扩展网站中的所有实际页面。通过这种方式,所有通用代码都可以保存在一个文件中,而且每次更新公共代码时都不必担心单独更新每一个页面。

<?php 
    abstract class AbstractPage { 
     // Constructor that children can call 
     protected function __construct() { 
     } 

     // Other functions that may be common 
     private function displayHeader() {} 
     private function displaySidebar() {} 
     private function displayFooter() {} 
     abstract protected function displayUniquePageInfo(); 

     public function display() { 
      $this->displayHeader(); 
      $this->displaySidebar(); 
      $this->displayUniquePageInfo(); 
      $this->displayFooter(); 
     } 

    } 

    // Next have a page that inherits from AbstractPage 
    public class ActualPage extends AbstractPage { 
     public function __construct() { 
      parent::__construct(); 
     } 

     // Override function that displays each page's different info 
     protected function displayUniquePageInfo() { 
      // code 
     } 
    } 

    // Actually display the webpage 
    $page = new ActualPage(); 
    $page->display(); 
?> 
相关问题