background image

php

 

     生成静态

 

     html

 

     文件原理

 

 

php 生成静态 html 文件的好处相信做过网站的人都知道,它能大大提高服务器的负载
能力,下面就来看看 php 生成静态 html 文件原理:

1.

<?

php

2. ob_start

();

3. include_once 

'index.php'

;

 

4. $buffer

=

ob_get_contents

();

5. ob_end_clean

();

 

//清空缓存

6. $fp

=

@fopen

(

"./index.html"

,

"w+"

);

 

7.

@fwrite

(

$fp

,

$buffer

);

8.

@fclose

(

$fp

);

9.

?>

 

这个例子的作用就是将 index.php 转化成 index.html,需要注意的是目录的权限,还有
待转化的文件不能有 ob_start()和 ob_end_clean()。

下面提供一个 php 生成静态 html 文件类:

1.

<?

php

2.

class

 

Shtml

3. {

4.    

public

 $Templet

;

5.    

public

 $DataSource

;

6.    

public

 $Dir

;

7.    

public

 $fileName

;

8.    

public

 $mod

;

9.    

public

 $handle

;

10.  
11.    

function

 __construct

(

$fileName 

=

 

""

)

12.    

{

13.    $this

->

fileName 

=

 $fileName

;

14.    $this

->

mod 

=

 

"wb"

;

15.    $this

->

handle 

=

 

false

;

16.  
17.    $this

->

Templet

 

=

 

""

;

18.    $this

->

DataSource

 

=

 array

();

19.    $this

->

Dir

 

=

 

""

;

20.    

}

21.