Example of PHP generating static page function (php2html)

Source: Internet
Author: User
  1. /**
  2. ------------------------
  3. Function:php2html ($in _url, $out _htmlfile, $out _logfile)
  4. ------------------------
  5. @ Description: Generate static function
  6. @ copyright:copyright (c) 2006-2011
  7. @ create:2006-08-01
  8. @ modify:2013-02-16
  9. @ Hint: The path is the absolute path of the server; Automatically created if the given path directory does not exist
  10. @ example:php2html ("http://bbs.it-home.org", "/www/html/index.html", "/www/log/log.txt");
  11. */
  12. {{{contents
  13. function php2html ($in _url, $out _htmlfile, $out _logfile)
  14. {
  15. $htmlContent = file_get_contents ($in _url); Read a file into $htmlContent variable
  16. /**
  17. * @ Check if the file to be generated exists
  18. */
  19. if (Is_file ($out _htmlfile))
  20. {
  21. @unlink ($out _htmlfile);//If the file already exists, delete
  22. }
  23. /**
  24. * @ Create a catalog page section
  25. */
  26. $dir _array = Explode ("/", DirName ($out _htmlfile));
  27. ChDir ("/"); Change Directory to root
  28. for ($i =1; $i<>
  29. {
  30. if (Is_dir ($dir _array[$i]))
  31. {
  32. ChDir ($dir _array[$i]);
  33. }
  34. Else
  35. {
  36. mkdir ($dir _array[$i]);
  37. ChDir ($dir _array[$i]);
  38. }
  39. }
  40. /**
  41. * @ Create directory log section
  42. */
  43. $dir _array = Explode ("/", DirName ($out _logfile));
  44. ChDir ("/"); Change Directory to root
  45. for ($i =1; $i<>
  46. {
  47. if (Is_dir ($dir _array[$i]))
  48. {
  49. ChDir ($dir _array[$i]);
  50. }
  51. Else
  52. {
  53. mkdir ($dir _array[$i], 0777);
  54. ChDir ($dir _array[$i]);
  55. }
  56. }
  57. $handle = fopen ($out _htmlfile, "w"); Open file pointer, create file
  58. $logHandle = fopen ($out _logfile, "A +"); Open log File
  59. /**
  60. * @ Check if directory is writable
  61. */
  62. if (!is_writable ($out _htmlfile))
  63. {
  64. echo "File:". $out _htmlfile. " Not writable, please check the directory properties and retry ";
  65. Exit ();
  66. }
  67. if (!is_writable ($out _logfile))
  68. {
  69. echo "File:". $out _logfile. " Not writable, please check the directory properties and retry ";
  70. Exit ();
  71. }
  72. /**
  73. * @ Write file
  74. */
  75. if (!fwrite ($handle, $htmlContent))
  76. {
  77. $LOGMSG = "Write file". $out _htmlfile. "Failure";
  78. }
  79. Else
  80. {
  81. $LOGMSG = "Create File". $out _htmlfile. "Success";
  82. }
  83. /**
  84. * Record Log
  85. */
  86. $logMsg. = "(". Date ("Y-m-d h:i:s"). ") \ r \ n ";
  87. Fwrite ($logHandle, $LOGMSG);
  88. Fclose ($logHandle); Close the log pointer
  89. Fclose ($handle); Close pointer
  90. }
  91. // }}}
  92. Php2html ("http://bbs.it-home.org", DirName (__file__). " /yanjing_html/index.html ", DirName (__file__). /yanjing_log/log.txt ");
  93. echo "Success";
  94. ?>
Copy Code

articles you might be interested in:three ways to generate static pages in PHP and code to explain how PHP generates static pages (three functions) PHP's method of generating an HTML static page reference PHP writes a class that generates a static page to generate static pages on a code virtual host that generates HTML static pages for all content in the database. PHP generates static pages in a detailed tutorial on how to automatically generate a page in the same way that PHP does not access pseudo-static pages on static pages Spider crawl record code smarty generate static page method PHP generates static page methods Apache cannot access pseudo-static page solutions

  • 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.