2011-03-06 95 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(); 
?> 
相關問題