Php provides multiple methods to generate static files. First, generate static copy code for php dynamic page content: ob_start (); # enable the server cache include_onceIndex.php; $ ctxob_get_contents (); # obtain the cache ob_end _
First: generate static php dynamic page content
The code is as follows:
Ob_start (); # enable server cache
Include_once 'index. php ';
$ Ctx = ob_get_contents (); # get cache
Ob_end_clean (); # clear the cache
$ Fh = fopen ("index.html", "w + ");
Fwrite ($ fh, $ ctx); # write html to generate html
Fclose ($ fh );
/*
1. Flush: refresh the buffer content and output it.
Function format: flush ()
Note: This function is frequently used and highly efficient.
2. ob_start: Open the output buffer.
Function format: void ob_start (void)
Note: When the buffer zone is activated, all non-file header information from the PHP program is not sent, but stored in the internal buffer zone. To output the buffer content, you can use ob_end_flush () or flush () to output the buffer content.
3. ob_get_contents: returns the content of the internal buffer.
Use
Function format: string ob_get_contents (void)
Note: This function returns the content in the current buffer. if the output buffer is not activated, FALSE is returned.
4. ob_get_length: return the length of the internal buffer.
Usage: int ob_get_length (void)
Note: This function returns the length of the current buffer. it is the same as ob_get_contents if the output buffer is not activated. Returns FALSE.
5. ob_end_flush: sends the content of the internal buffer to the browser and closes the output buffer.
Usage: void ob_end_flush (void)
Note: This function sends the content of the output buffer (if any ).
6. ob_end_clean: delete the content of the internal buffer and disable the internal buffer.
Usage: void ob_end_clean (void)
Note: This function will not output the content of the internal buffer but delete it!
7. ob_implicit_flush: enable or disable absolute refresh
Usage: void ob_implicit_flush ([int flag])
*/
Second:
Php static file generation class (for users)
The code is as follows:
Class CreateHtml
{
Function mkdir ($ prefix = 'article ')
{
$ Y = date ('Y ');
$ M = date ('M ');
$ D = date ('D ');
$ P = DIRECTORY_SEPARATOR;
$ FilePath = 'article'. $ p. $ y. $ p. $ m. $ p. $ d;
$ A = explode ($ p, $ filePath );
Foreach ($ a as $ dir)
{
$ Path. = $ dir. $ p;
If (! Is_dir ($ path ))
{
// Echo 'this directory does not exist'. $ path;
Mkdir ($ path, 0755 );
}
}
Return $ filePath. $ p;
}
Function start ()
{
Ob_start ();
}
Function end ()
{
$ Info = ob_get_contents ();
$ FileId = '000000 ';
$ Postfix = '.html ';
$ Path = $ this-> mkdir ($ prefix = 'article ');
$ FileName = time (). '_'. $ fileId. $ postfix;
$ File = fopen ($ path. $ fileName, 'W + ');
Fwrite ($ file, $ info );
Fclose ($ file );
Ob_end_flush ();
}
}
?>
$ S = new CreateHtml ();
$ S-> start ();
?>
Asdfasdfasdfasdfasdfasdfasdfasdfasdfasdf
Adfasdfasdf
>
$ S-> end ();
?>
The authorization code is as follows: ob_start (); # enable the server cache include_once 'index. php'; $ ctx = ob_get_contents (); # obtain the cache ob_end _...