I need to rescr开发者_Python百科ipt every file on my website by putting a footer at the bottom.
Give this a try:
This will add the line $footer_code
to the end of all php
files in $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);
}
?>
If this is some boilerplate code that all of your pages need, then might I suggest using some sort of abstract class that all of the actual pages in your website extend. This way all of the common code can be kept in one file and you don't have to worry about separately updating every single one of your pages every time you have an update for the common code.
<?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();
?>
There is an Apache module lets you set a common footer for every file served, check this for mroe -> http://freshmeat.net/projects/mod_layout/
精彩评论