Php code for automatically publishing news. The news file is stored in a fixed directory in the format of the ultimate file. txt, and the publishing of news can be completed automatically. This process consists of two parts: The first is the display of news. the shownews. news file is stored in a fixed directory in the format of the ultimate file. txt, and the publishing of news can be completed automatically.
This process consists of two parts:
First, the display of news is implemented by the shownews. php script. the code is as follows:
<Table border = "0" width = "90%"> "〉
<〈? Php
// The title of the news in the first line of the News file
// The Newsfile contains the Upload file (.txt)
$ Newspath = "./news/"; // modify the news file directory
$ 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 = "<a href ="./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> 〉
Put the code in the area where news is displayed:
<〈? Php
Require "./shownews. php ";
? > 〉
The second part is the publishing of news, which is implemented by the pupnews. php script. the code is as follows:
<〈? Php
If ($ id = "")
{
Header ("Location:./shownews. php ");
}
? > 〉
<Html> 〉
<Head> 〉
<Meta content = "chenqiang" name = Author> 〉
<〈? Php
$ Filename = "./news/". $ id. ". txt ";
$ Fa = file ($ filename );
$ N = count ($ fa );
$ S = trim ($ fa [0]);
$ S = htmlspecialchars ($ s );
$ T = "-news by waterwall ";
Print "<title>". $ s. $ t. "</title> n ";
? > 〉
</Head> 〉
<Body> 〉
<〈? Php
// Output text title
Print "<blockquote> n ";
Print "<B> <center>". $ s. "n ";
Print "</center> </B> 〉
<P> n ";
// Output text body
For ($ I = 1; $ I <$ n; $ I + = 1)
{
$ S = chop ($ fa [$ I]);
$ S = htmlspecialchars ($ s );
$ S = trim ($ s );
Print "". $ s ."
N ";
}
Print "</p> </blockquote> n ";
? > 〉
</Body> 〉
</Html> 〉
It can also be implemented using a database, which is just a file implementation method.
Bytes. This process consists of two parts: The first is the display of news, by shownews ....