Home  >  Article  >  Backend Development  >  Sharing multiple methods of generating static files in PHP

Sharing multiple methods of generating static files in PHP

高洛峰
高洛峰Original
2016-12-01 09:57:581111browse

The first type: Generate static php dynamic page content
Copy the code The code is as follows:
ob_start();#Turn on the server cache
include_once 'Index.php';
$ctx=ob_get_contents();#Get the cache
ob_end_clean() ;#Clear cache
$fh=fopen("index.html","w+");
fwrite($fh,$ctx);#Write html, generate html
fclose($fh);
/*
1 , Flush: refresh the contents of the buffer and output.
Function format: flush()
Description: This function is frequently used and is very efficient.
2. ob_start: Open the output buffer
Function format: void ob_start(void)
Description: When the buffer is activated, all non-file header information from the PHP program will not be sent, but will be saved in the internal buffer. In order to output the contents of the buffer, you can use ob_end_flush() or flush() to output the contents of the buffer.
3, ob_get_contents: Return the contents of the internal buffer.
Use
Function format: string ob_get_contents(void)
Description: This function will return the contents of the current buffer. If the output buffer is not activated, it will return 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 in the current buffer; like ob_get_contents, if the output buffer is not activated. then returns FALSE.
5. ob_end_flush: Send the contents of the internal buffer to the browser and close the output buffer.
Usage: void ob_end_flush(void)
Description: This function sends the contents of the output buffer (if any).
6. ob_end_clean: Delete the contents of the internal buffer and close the internal buffer
Usage: void ob_end_clean(void)
Description: This function will not output the contents of the internal buffer but delete it!
7. ob_implicit_flush: Turn on or off absolute refresh
Usage method: void ob_implicit_flush ([int flag])
*/

Second type:
php static file generation class (for home use)
Copy code 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 'No such 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();
?>


asdfasdfasdfasdfasdfasdfasdfasdfasdf

adfasdfas df



$s->end();
?>

Statement:
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