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

Sharing multiple methods of generating static files in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:17:56
Original
973 people have browsed it

The first method: Generate php dynamic page content into static

Copy the code The code is as follows:

ob_start ();#Enable server cache
include_once 'Index.php';
$ctx=ob_get_contents();#Get 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, 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 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: Returns 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; the same as 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 method: 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();
}
}
?>
$s=new CreateHtml();
$s->start() ;
?>


asdfasdfasdfasdfasdfasdfasdfasdf

adfasdfasdf
🎜>< /html>
$s->end();
?>


http://www.bkjia.com/PHPjc/325634.html

truehttp: //www.bkjia.com/PHPjc/325634.htmlTechArticleThe first method: generate static copy code from php dynamic page content. The code is as follows: ob_start();#Enable server caching include_once 'Index.php'; $ctx=ob_get_contents();#Get cache ob_end_...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template