Heim  >  Artikel  >  Backend-Entwicklung  >  php生成静态页面函数(php2html)的例子

php生成静态页面函数(php2html)的例子

WBOY
WBOYOriginal
2016-07-25 09:05:49728Durchsuche
  1. /**
  2. ------------------------
  3. Function: php2html($in_Url, $out_htmlFile, $out_logFile)
  4. ------------------------
  5. @ Description: 生成静态函数
  6. @ Copyright: Copyright (c) 2006 - 2011
  7. @ Create: 2006-08-01
  8. @ Modify: 2013-02-16
  9. @ 提示:路径为服务器绝对路径; 若给定的路径目录不存在则自动创建
  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); //将文件读入 $htmlContent 变量
  16. /**
  17. * @检查要生成的文件是否存在
  18. */
  19. if (is_file($out_htmlFile))
  20. {
  21. @unlink($out_htmlFile);//若文件已存在,则删除
  22. }
  23. /**
  24. * @ 创建目录 网页部分
  25. */
  26. $dir_array = explode("/", dirname($out_htmlFile));
  27. chdir("/"); //改变目录到根
  28. for($i=1;$i{
  29. if(is_dir($dir_array[$i]))
  30. {
  31. chdir($dir_array[$i]);
  32. }
  33. else
  34. {
  35. mkdir($dir_array[$i]);
  36. chdir($dir_array[$i]);
  37. }
  38. }
  39. /**
  40. * @ 创建目录 日志部分
  41. */
  42. $dir_array = explode("/", dirname($out_logFile));
  43. chdir("/"); //改变目录到根
  44. for($i=1;$i{
  45. if(is_dir($dir_array[$i]))
  46. {
  47. chdir($dir_array[$i]);
  48. }
  49. else
  50. {
  51. mkdir($dir_array[$i], 0777);
  52. chdir($dir_array[$i]);
  53. }
  54. }
  55. $handle = fopen($out_htmlFile, "w"); //打开文件指针,创建文件
  56. $logHandle = fopen ($out_logFile, "a+"); //打开日志文件
  57. /**
  58. * @检查目录是否可写
  59. */
  60. if (!is_writable($out_htmlFile))
  61. {
  62. echo "文件:".$out_htmlFile."不可写,请检查目录属性后重试";
  63. exit();
  64. }
  65. if (!is_writable($out_logFile))
  66. {
  67. echo "文件:".$out_logFile."不可写,请检查目录属性后重试";
  68. exit();
  69. }
  70. /**
  71. * @写入文件
  72. */
  73. if (!fwrite ($handle, $htmlContent))
  74. {
  75. $logMsg = "写入文件" . $out_htmlFile . "失败";
  76. }
  77. else
  78. {
  79. $logMsg = "创建文件" . $out_htmlFile . "成功";
  80. }
  81. /**
  82. * @记录日志
  83. */
  84. $logMsg .= "(".date("Y-m-d H:i:s") .")\r\n";
  85. fwrite ($logHandle, $logMsg);
  86. fclose($logHandle); //关闭日志指针
  87. fclose ($handle); //关闭指针
  88. }
  89. // }}}
  90. php2html("http://bbs.it-home.org", dirname(__FILE__)."/yanjing_html/index.html", dirname(__FILE__)."/yanjing_log/log.txt");
  91. echo "成功";
  92. ?>
复制代码

您可能感兴趣的文章: php生成静态页面的三种方法与代码详解 php生成静态页面的方法(三个函数) php生成html静态页面的方法参考 php写的一个生成静态页面的类 将数据库中的所有内容生成html静态页面的代码 虚拟主机上定时自动生成静态页面的方法 php生成静态页面的详细教程 apache中访问不了伪静态页面的解决方法 php写的关于静态页面的蜘蛛爬行记录的代码 smarty生成静态页面的方法 PHP生成静态页面的方法 apache访问不了伪静态页面的解决方法



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn