Two ways to generate static files in PHP

Source: Internet
Author: User
Tags php write
    1. Ob_start (); #开启服务器缓存
    2. Include_once ' index.php ';
    3. $ctx =ob_get_contents (); #获取缓存
    4. Ob_end_clean (); #清空缓存
    5. $FH =fopen ("index.html", "w+");
    6. Fwrite ($FH, $ctx); #写入html, generating HTML
    7. Fclose ($FH);
    8. ?>
Copy Code

Method 2:php static file generation class

  1. Class createhtml
  2. {
  3. function mkdir ($prefix = ' article ')
  4. {
  5. $y = date (' Y ');
  6. $m = Date (' m ');
  7. $d = Date (' d ');
  8. $p =directory_separator;
  9. $filePath = ' article '. $p. $y $p. $m. $p. $d;
  10. $a =explode ($p, $filePath);
  11. foreach ($a as $dir)
  12. {
  13. $path. = $dir. $p;
  14. if (!is_dir ($path))
  15. {
  16. Echo ' does not have this directory '. $path;
  17. mkdir ($path, 0755);
  18. }
  19. }
  20. return $filePath. $p;
  21. }
  22. function Start ()
  23. {
  24. Ob_start ();
  25. }
  26. Function End ()
  27. {
  28. $info = Ob_get_contents ();
  29. $fileId = ' 12345 ';
  30. $postfix = '. html ';
  31. $path = $this->mkdir ($prefix = ' article ');
  32. $fileName = Time (). ' _ '. $fileId. $postfix;
  33. $file =fopen ($path. $fileName, ' w+ ');
  34. Fwrite ($file, $info);
  35. Fclose ($file);
  36. Ob_end_flush ();
  37. }
  38. }
  39. ?>
  40. $s =new createhtml ();
  41. $s->start ();
  42. ?>
  43. Asdfasdfasdfasdfasdfasdfasdfasdfasdf
  44. Adfasdfasdf
  45. >
  46. $s->end ();
  47. ?>
Copy Code

Attached: 1, flush: Flush the contents of the buffer, output. function format: Flush () Description: This function is often used and is highly efficient.

2, Ob_start: Open output buffer function format: void Ob_start (void) Description: When the buffer is activated, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. In order to output the contents of the buffer, you can use the contents of the Ob_end_flush () or flush () output buffers.

3. Ob_get_contents: Returns the contents of the internal buffer. Use function Format: string ob_get_contents (void) Description: This function returns the contents of the current buffer and returns FALSE if the output buffer is not activated.

4. Ob_get_length: Returns the length of the internal buffer. Usage: int ob_get_length (void) Description: This function returns the length in the current buffer, as in ob_get_contents if the output buffer is not active. FALSE is returned.

5. Ob_end_flush: Sends the contents of the internal buffer to the browser, and closes the output buffer. Method of Use: 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 use method: void Ob_end_clean (void) Description: This function does not output the contents of the internal buffer but deletes it!

7. Ob_implicit_flush: Open or close the absolute refresh using method: void Ob_implicit_flush ([int flag])

you may be interested in the article:three ways to generate static pages in PHP and the code in detail PHP generates static page functions (php2html) example PHP method for generating static pages (three functions) The template of PHP generated static files and cache PHP write a static page generated on the class virtual host on a regular basis to automatically generate static pages of PHP static pages in a detailed tutorial on how to generate a static HTML file in PHP Smarty method to generate static pages learn how PHP generates static HTML files. PHP generation static Method of the page PHP three ways to generate a static HTML file

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.