|
|
|
|
Like this code?
Show the author your appreciation.
|
|
| |
Easy textfile news system w/out categories.
This script is an easy way to have a text based news system.
It is very simple to customize and will build the folder structure itself. All you have to upload to your webspace is this one file after providing a password for the "add news" form.
Simple, straight forward, easy to use.
Hope you like it!
Chris
| <?php
/*
Easy textfile news system
This script is an easy way to have a text based news system.
To add news, call index.php?add=true in your browser. Please provide a password
in the function showaddform().
The system will generate a folder structure like the folloing
Year Month Day Newsfile
------------------------------------------
2007 -
|- 01 -|
|- 30 -|
|-timestamp.news
|-timestamp.news
|-timestamp.news
|- 31
|- 02
|- 1
|- 2
|- 03
|- etc
In the newsfile, the first line is the title, second is the header text and third and following are regular text lines
Programmed by Christian Haensel, christian@chftp.com, http://www.chftp.com
Exclusively published on weberdev.com
If you like my scripts, please let me know or link to me.
You may copy, redistirubte, change and alter my scripts as long as this information remains intact
*/
function datefolders() {
// This function checks the existance of the news directory for this day
// and, if necessary, creates the directories for year, month and day.
$checkyear = date("Y");
$checkmonth = date("m");
$checkday = date("d");
// Checking for this year's folder
if(!is_dir("news/".$checkyear)) {
mkdir("news/".$checkyear,0755);
}
// Checking for this month's folder
if(!is_dir("news/".$checkyear."/".$checkmonth)) {
mkdir("news/".$checkyear."/".$checkmonth,0755);
}
// Checking for today's folder
if(!is_dir("news/".$checkyear."/".$checkmonth."/".$checkday)) {
mkdir("news/".$checkyear."/".$checkmonth."/".$checkday,0755);
}
}
function readnews($day,$month,$year,$number) {
$nfile = file("news/".$year."/".$month."/".$day."/".$number);
$i = 1;
$date_ex = explode(".", $number);
$timest = $date_ex[0];
$news_date = date("d.m.Y - H:i", $timest);
foreach($nfile as $nline) {
if($i==1) {
echo '<h1>'.$nline.'</h1>';
} elseif($i==2) {
echo '<h3>'.$nline.'</h3>';
} else {
echo $nline."<br>";
}
$i++;
}
echo '<p><small>Posted '.$news_date.'</small><p>';
echo '--------------------------------------------------------------';
}
function check_news() {
// This function checks for news in the directories. It will take URL-Parameters
// like day, month and year. If thos are not set, it will take the current date
if(eregi("\.", $_GET['day']) || eregi("\.", $_GET['month']) || eregi("\.", $_GET['year'])) {
die("No hacking");
}
if(isset($_GET['year'])) {
$newsyear = $_GET['year'];
} else {
$newsyear = date("Y");
}
if(isset($_GET['month'])) {
$newsmonth = $_GET['month'];
} else {
$newsmonth = date("m");
}
if(isset($_GET['day'])) {
$newsday = $_GET['day'];
} else {
$newsday = date("d");
}
// Check for news in the directory
$news_arr = array();
$newsfolder = @opendir("news/".$newsyear."/".$newsmonth."/".$newsday) or die("No news for this time");
while($news = readdir($newsfolder)) {
if(eregi(".news", $news)) {
array_push($news_arr, $news);
}
}
sort($news_arr);
$news_arr = array_reverse($news_arr);
foreach($news_arr as $news) {
readnews($newsday,$newsmonth,$newsyear,$news);
}
}
function showaddform() {
// Set the access password here
$mypass = "mypassword";
if(!isset($_POST['pass'])) {
echo '
<form name="login" action="#" method="POST";
Login:
<input type="password" name="pass">
<input type="submit" value="Login">
</form>';
die;
}
if(isset($_POST['pass']) && $_POST['pass'] != $mypass) {
echo '
Login failed.<p>
<form name="login" action="#" method="POST";
Login:
<input type="password" name="pass">
<input type="submit" value="Login">
</form>';
die;
} elseif(isset($_POST['pass']) && $_POST['pass'] == $mypass) {
echo '
<div style="padding:5px; border:1px solid #000000;">
<b>Add News</b><p>
<form name="addnews" action="#" method="POST">
<table>
<tr>
<td>Title:</td><td><input type="text" name="title">
</td>
</tr>
<tr>
<td>
Header:</td><td><input type="text" name="header">
</td>
</tr>
<tr>
<td>
Text:</td><td><textarea name="text"></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<input type="Submit" value="Add news">
<input type="hidden" name="pass" value="'.$_POST[pass].'">
<input type="hidden" name="action" value="addnews">
</td>
</tr>
</table>
</form>
</div>
';
}
}
function doadd() {
$nowyear = date("Y");
$nowmonth = date("m");
$nowday = date("d");
$ntitle = $_POST['title'];
$nheader = $_POST['header'];
$ntext = $_POST['text'];
$ntimest = time();
$newfile = fopen("news/".$nowyear."/".$nowmonth."/".$nowday."/".$ntimest.".news", "w+");
$newstext = $ntitle."\n".$nheader."\n".$ntext;
fwrite($newfile, $newstext);
fclose($newfile);
echo 'News added';
}
function checkadd() {
if($_GET['add'] == "true") {
showaddform();
}
if($_POST['action'] == "addnews") {
doadd();
}
}
// Do it all
checkadd(); datefolders(); check_news();
?> | |
|
|
| Display list of files within current and subdirectories (recursively) showing
each file as an anchored link and each directory as a category header. Categories : Filesystem, Directories, Arrays, PHP | | | Variable serialization and unserialization. Loading and saving variable structures
to and from file. Categories : Arrays, Filesystem, Variables, Strings, PHP | | | Filling an array with files from any given directory. This example is for the current PHP script's directory. Categories : PHP, Arrays, Filesystem | | | How to ifconfig down/up a list of IP's Categories : Arrays, Strings, Filesystem, PHP | | | Compare two texts and display a block of text with the differences between them. Categories : PHP, PHP Classes, Filesystem, Strings, Arrays | | | Grab images from one or more URLs and save them to a specified local directory. Categories : PHP, Filesystem, Strings, Arrays | | | A function which places the path and name of all subdirectories into an array Categories : PHP, Filesystem, Arrays, Directories | | | This script will read all images from a folder and read the files into an array. It uses rand() to get a random number. It will display a random image from the image folder given. Categories : PHP, Arrays, Graphics, Filesystem | | | PHP4 DirectoryIterator Class Categories : PHP, PHP Classes, Filesystem, Directories | | | Keep() - maintenance function for backup folders Categories : PHP, Filesystem, Maintenance | | | Contents Page - a script to build contents pages. Categories : Complete Programs, PHP, Utilities, Filesystem | | | PHP Random rss feeds - selects 49 random feeds from an unlimited list and displays them on your website. It's Ideal for those moments when you got 5 minutes and dont know which one of your feeds to read. Categories : PHP, Rich Site Summary (RSS), Arrays | | | Looping through two arrays Categories : PHP, Databases, Arrays | | | How to create an empty file? (touch) Categories : Filesystem, PHP | | | Simple way of scaling any image to fit either given width or height. Categories : PHP, Graphics, Arrays | |
|
|
|