〖自动发布新闻的php代码〗
作者:waterwall
来源:奥索网
新闻文件以文本文件.txt的格式存放在一个固定的目录下,新闻的发布可以自动完成。
该过程由两部分组成:
第一是新闻的显示,由shownews.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 "
n";$fa = file($newspath.$key);
$s=trim($fa[0]);
$s=htmlspecialchars($s);
$lk=strlen($key);
$a=substr($key,0,$lk-4);
$s="".$s."";
print $s." n";
print "(".date("Y年m月d日 - H:i:s",$newsfile[$key]).")
n";
print "
";}
$hd->close();
?>
在显示新闻的地方放入代码:
require "./shownews.php";
?>
第二部分为新闻的发布,由pupnews.php脚本实现,代码如下:
if ($id=="")
{
Header("Location: ./shownews.php");
}
?>
$filename="./news/".$id.".txt";
$fa=file($filename);
$n=count($fa);
$s=trim($fa[0]);
$s=htmlspecialchars($s);
$t=" - news by waterwall";
print "
".$s.$t."n";?>
//输出文本标题
print "
n";n";print "
".$s."n"; print "
n";
//输出文本正文
for ($i=1;$i
{
$s=chop($fa[$i]);
$s=htmlspecialchars($s);
$s=trim($s);
print " ".$s."
n";
}
print "
?>
用数据库也可实现,这只是文件形式的实现方法。
责任编辑: