background image

自动发布新闻的

php 代码示例

新闻文件以文本文件

.txt 的格式存放在一个固定的目录下,新闻的发布可以自动完成。

该过程由两部分组成

:

第一是新闻的显示,由

shownews.PHP 脚本实现,代码如下:

tableborder="0"width="90%"〉

?php

//在新闻文件中的第一行放新闻的标题
//新闻文件必须是文本文件(.txt)
$newspath="./news/";//修改新闻文件存放的目录
$newsfile=array();
$hd=dir($newspath);
while($filename=$hd-〉read()){
$s=strtolower($filename);
if(strstr($s,".txt")){
$lastchanged=filemtime($newspath.$filename);
$newsfile[$filename]=$lastchanged;
}
}
arsort($newsfile);
for(reset($newsfile);$key=key($newsfile);next($newsfile)){
print"〈tr〉〈td〉n";
$fa=file($newspath.$key);
$s=trim($fa[0]);
$s=htmlspecialchars($s);
$lk=strlen($key);
$a=substr($key,0,$lk-4);
$s="〈ahref="./pubnews.php?id=".$a.""target=_blank〉".$s."〈/a〉";
print$s."n";
print"(".date("Y 年 m 月 d 日-H:i:s",$newsfile[$key]).")
n";
print"〈/td〉〈/tr〉";
}
$hd-〉close();
?〉

/table〉