How to implement automatic generation of code for php static news list

WBOY
Release: 2016-07-25 09:00:55
Original
971 people have browsed it
Many PHP newbies are looking for ways to generate static news lists. Here is a code that automatically generates static lists for your reference.

It is introduced in many php tutorials, only examples are provided here.

<?php
/**
 * php 静态新闻列表 自动生成代码
 * site bbs.it-home.org
*/
function CreateShtml()
{
ob_start(array("callback_CreateShtml","callback_GoToShtml"));
}

function callback_CreateShtml($buffer)
{
$page = intval(@$_REQUEST["page"]);
$fileName = $_SERVER['DOCUMENT_ROOT'] . dirname($_SERVER['PHP_SELF']) . "/" . basename($_SERVER['PHP_SELF'],".php") . ($page==0 ? "" : "_" . strval($page)) . ".htm";
$fp = fopen($fileName,"wb");
fwrite($fp,$buffer);
fclose($fp);
return $buffer;
}

function callback_GoToShtml($buffer)
{
$page = intval(@$_REQUEST["page"]);
$fileName = basename($_SERVER['PHP_SELF'],".php") . ($page==0 ? "" : "_" . strval($page)) . ".htm";
header("location:" . $fileName);
return $buffer;
}

function GoToShtml()
{
$page = intval(@$_REQUEST["page"]);
$fileName = basename($_SERVER['PHP_SELF'],".php") . ($page==0 ? "" : "_" . strval($page)) . ".htm";
if(file_exists($fileName))
header("location:" . $fileName);
}

function DeleteShtml($fileName=NULL)
{
if(is_null($fileName))
$fileName = $_SERVER['DOCUMENT_ROOT'] . $_SERVER['PHP_SELF'];

if($fileName[0]=="/")
$fileName = $_SERVER['DOCUMENT_ROOT'] . $fileName;

$path = dirname($fileName);
$dir = dir($path);
$patten = "/^" . basename($fileName, ".php") . "(_[0-9]+)?.htm/";
while(($entry = $dir->read())!==false)
{
if(is_file($path . "/" .$entry) && preg_match($patten,$entry))
unlink ($path . "/" . $entry);
}
} 
?>
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!