Multiple ways to generate static files are Shared by php
- 2020-05-17 05:00:25
- OfStack
Type 1: generate static php dynamic page content
The second:
php static file generation class (for home use)
ob_start();# Enable server cache
include_once 'Index.php';
$ctx=ob_get_contents();# Access to the cache
ob_end_clean();# Clear the cache
$fh=fopen("index.html","w+");
fwrite($fh,$ctx);# write html, generate html
fclose($fh);
/*
1 , Flush : refreshes the contents of the buffer and outputs.
Function format: flush()
Note: this function is often used, high efficiency.
2 , ob_start : opens the output buffer
Function format: void ob_start(void)
Note: when the buffer is activated, all from PHP The program's non-file header information is not sent, but is stored in an internal buffer. To output the contents of the buffer, use ob_end_flush() or flush() Output the contents of the buffer.
3 , ob_get_contents : returns the contents of the internal buffer.
use
Function format: string ob_get_contents(void)
Note: this function returns the contents of the current buffer, or if the output buffer is not active FALSE .
4 , ob_get_length : returns the length of the internal buffer.
Usage: int ob_get_length(void)
Description: this function will return the length of the current buffer; and ob_get_contents1 If the output buffer is not activated. It returns FALSE .
5 , ob_end_flush : sends the contents of the internal buffer to the browser and closes the output buffer.
Usage: void ob_end_flush(void)
Note: this function sends the contents of the output buffer, if any.
6 , ob_end_clean : deletes the contents of the internal buffer and closes the internal buffer
Usage: void ob_end_clean(void)
Note: this function does not output the contents of the internal buffer but deletes it!
7 , ob_implicit_flush : turns absolute refresh on or off
Usage: void ob_implicit_flush ([int flag])
*/
The second:
php static file generation class (for home use)
<?php
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 ' We don't have this directory '.$path;
mkdir($path,0755);
}
}
return $filePath.$p;
}
function start()
{
ob_start();
}
function end()
{
$info = ob_get_contents();
$fileId = '12345';
$postfix = '.html';
$path = $this->mkdir($prefix= 'article');
$fileName = time().'_'.$fileId.$postfix;
$file=fopen($path.$fileName,'w+');
fwrite($file,$info);
fclose($file);
ob_end_flush();
}
}
?>
<?php
$s=new CreateHtml();
$s->start();
?>
<html>
<body>
asdfasdfasdfasdfasdfasdfasdfasdfasdf<br>
adfasdfasdf<br>
</body>>
</html>
<?php
$s->end();
?>