gaoshikao
新手上路

UID 82076
精华
0
积分 25
帖子 20
金钱 25 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2006-10-24
状态 离线
|
静态页面生成之方案
随着网络的发展,SEO逐渐得到了我们的重视,把网站做成静态网站是势在必行了.生成静态页面的方法不外乎几种,看了很多资料,都是介绍通过OB函数控制输出的,我在这里给大家介绍我的方法: 通过file_get_contents()方法.这个方法可以获取网页的内容,包括本地的和internet上的,所以我们可以通过$content = file_get_contents("http://localhost/index.php");获取本地的或者防在服务器上的文件输出结果,然后创建指定的目录和文件,最后把内容写进去,从而实现了静态页面的输出.在这里,我发两个函数,一个是写函数,一个是读函数.完全兼容php4和php5.
function dfile_put_contents($file, &$data, $flags=0){
// Check to see if functin exists
if (function_exists('file_put_contents')) {
file_put_contents($file, $data);
}else{
// Define constants used by function, if not defined
if (!defined('FILE_USE_INCLUDE_PATH')) define('FILE_USE_INCLUDE_PATH', 1);
if (!defined('FILE_APPEND')) define('FILE_APPEND', 8);
// Varify arguments are correct types
if (!is_string($file)) return(false);
if (!is_string($data) && !is_array($data)) return(false);
if (!is_int($flags)) return(false);
// Set the include path and mode for fopen
$include = false;
$mode = 'wb';
// If data in array type..
if (is_array($data)) {
// Make sure it's not multi-dimensional
reset($data);
while (list(, $value) = each($data)) {
if (is_array($value)) return(false);
}
unset($value);
reset($data);
// Join the contents
$data = implode('', $data);
}
// Check for flags..
// If include path flag givin, set include path
if ($flags&FILE_USE_INCLUDE_PATH) $include = true;
// If append flag givin, set append mode
if ($flags&FILE_APPEND) $mode = 'ab';
// Open the file with givin options
if (!$handle = @fopen($file, $mode, $include)) return(false);
// Write data to file
if (($bytes = fwrite($handle, $data)) === false) return(false);
// Close file
fclose($handle);
// Return number of bytes written
return($bytes);
}
}
function dfile_get_contents($file, $include=false){
// Check to see if functin exists
if (function_exists('file_get_contents')) {
return @file_get_contents($file);
}else{
// Varify arguments are correct types
if (!is_string($file)) return(false);
if (!is_bool($include)) return(false);
// Open the file with givin options
if (!$handle = @fopen($file, 'rb', $include)) return(false);
// Read data from file
$contents = fread($handle, filesize($file));
// Close file
fclose($handle);
// Return contents of file
return($contents);
}
}
|
|