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

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

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

 1   
 2include("makestr.php";   
 3include("head.php");   
 4$newspath="/announce/"; //以文本文件存放的新闻文件的目录   
 5$newsfile=array();//准备新闻数组   
 6$hd=dir($newspath); //目录句柄   
 7while($filename=$hd->read()){ //获取全部文件   
 8$s=strtolower($filename);   
 9if(strstr($s,".txt")){   
10//检测最新的修改日期   
11$lastchanged=fileatime($newspath.$filename);   
12$newsfile[$filename]=$lastchanged;   
13}   
14}   
15arsort($newsfile); //文件按时间排序   
16//输出文件   
17for(reset($newsfile);$key=key($newsfile);next($newsfile))   
18{$fa=file($newspath.$key);   
19$n=count($fa);   
20echo "

<p>".date("d.m.Y-H:i:s".$newsfile[$key])."<br/>\n";
for($i=0;$i&lt;$n;$i=$i+1){
$s=chop($fa[$i]);//去除空格
$s=htmlspecialchars($s);
print $s."</p>

1\n";   
2  
3}   
4}   
5$hd->close(); //释放句柄   
6include("tail.php");   

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

Published At
Categories with Web编程
Tagged with
comments powered by Disqus