Two methods for generating static files in php

Source: Internet
Author: User
Two methods for generating static files in php

  1. Ob_start (); # enable server cache
  2. Include_once 'index. php ';
  3. $ Ctx = ob_get_contents (); # get cache
  4. Ob_end_clean (); # clear the cache
  5. $ Fh = fopen ("index.html", "w + ");
  6. Fwrite ($ fh, $ ctx); # write html to generate html
  7. Fclose ($ fh );
  8. ?>

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 'this directory does not exist'. $ 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 = '000000 ';
  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. Asdfasdfasdfasdfasdfasdfasdfasdfasdfasdf
  44. Adfasdfasdf
  45. >
  46. $ S-> end ();
  47. ?>

Appendix: 1. Flush: refresh the buffer content and output it. Function format: flush () Description: This function is frequently used and highly efficient.

2. ob_start: format of the function used to open the output buffer: void ob_start (void) description: When the buffer is activated, all non-file header information from the PHP program is not sent, but saved 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. Function format: string ob_get_contents (void) Description: 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) Description: 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) Description: 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 use of the internal buffer. void ob_end_clean (void): This function will not delete the content of the internal buffer!

7. ob_implicit_flush: How to enable or disable absolute refresh: void ob_implicit_flush ([int flag])

Articles you may be interested in: three methods for generating static pages in php and code details examples of generating static page functions in php (php2html) php static file generation template and cache a static page generation class virtual host written by php to automatically generate static pages on a regular basis php to generate static pages detailed tutorial php generate static html files how php generates static HTML files. how PHP generates static pages. how php generates static html files. how php generates static pages? how PHP generates static HTML files?

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.