PHP个人网站架设连环讲(三)

  三 首页新闻发布,让你更新更轻松(中)

  上次我们做了一个文件头(至于文件尾,请大家自己做,假设为tail.php),一个函数的模块,现在,我们来一个基本功能的实现,也就是动态发布啦

  <?php

  include("makestr.php";

  include("head.php");

  $newspath="/announce/"; //以文本文件存放的新闻文件的目录

  $newsfile=array();//准备新闻数组

  $hd=dir($newspath); //目录句柄

  while($filename=$hd->read()){ //获取全部文件

  $s=strtolower($filename);

  if(strstr($s,".txt")){

  //检测最新的修改日期

  $lastchanged=fileatime($newspath.$filename);

  $newsfile[$filename]=$lastchanged;

  }

  }

  arsort($newsfile); //文件按时间排序

  //输出文件

  for(reset($newsfile);$key=key($newsfile);next($newsfile))

  {$fa=file($newspath.$key);

  $n=count($fa);

  echo "<p>".date("d.m.Y-H:i:s".$newsfile[$key])."<br>\n";

  for($i=0;$i<$n;$i=$i+1){

  $s=chop($fa[$i]);//去除空格

  $s=htmlspecialchars($s);

  print $s."</p>\n";

  }

  }

  $hd->close(); //释放句柄

  include("tail.php");

  ?>

  这样,将你的新闻文本传上你根目录的annouce子目录下,就可以方便发布新闻了。但真正的方便还不在于这,比如说,当新闻过时的时候,程序能自动删除它,多好。不用ftp,直接在线写下要新发的公告,多方便。好了,且听下回分解。