How do I add new articles/stories etc to my web site so that only the first whatever number of words (commonly known as a 'teaser') are displayed and that they are links to the full article/story? And is it possible to do using either mysql or a flat file?

Piece of cake!

First let's look at the flat file method.

The thought process:

Now the actual scripting.

For the purposes of this demonstration, we will be creating and using four primary files:

  1. The upload form, which we will call story_up.php
  2. The script to process the article/story, which we will call story_process.php
  3. The flat file, which we will call story.dat
  4. And finally, the script to display the teasers as links, which we will call teaser_display.php
Let's discuss the story.dat file first.

For all intent and purposes, this file is going to act as our 'database'. We want to use it to hold some important information about the article/story

All three of these will be on the same line of the story.dat file, and will be separated from each other by the pipe - |.

Ok, here is the story_up.php file:

this is the story_up.php file
note the use of: enctype="multipart/form-data"

if you were going to adapt this to use in a photo gallery 
type application, you could add some extra fields like name, 
description etc to add to your flatfile/database
<form enctype="multipart/form-data" action="story_process.php" method="post">
File to upload: <input name="uploadfile" type="file">
<input name= "upload" type="submit" value="Upload File">

Now the story_process.php file (the story.dat file is used in this script)

This script.

  1. It gets the current date and converts it into a string to be used as part of the file name
  2. Gets the original name of the article/story file, adds the date string and checks to see if that file name exists. If it does then it 'adjusts' the name.
  3. Places the article/story file in the appropriate folder
  4. Checks to see if the story.dat file exists (if not then it creates it). Edits the flat file to reflect the addition of a new article/story



function write_beg($filename$data){
//Imports old data
$handle fopen($filename"r");
$old_content fread($handlefilesize ($filename));

//Sets up new data
$final_content $data.$old_content;

//Writes new data
$handle2 fopen($filename"w");
$finalwrite fwrite($handle2$final_content);

strnpos($h_stk$n_dle$o_cur$pos 0) {
 for (
$i 1$i <= $o_cur$i++) {
$pos strpos($h_stk$n_dle$pos) + 1;
$pos 1;

//    set the number of words for the teaser
$max_words 30;

//    set the path to the story folder
$story_path "stories";

// get the date and convert to string
$pre_fix date('Ymd');

//    get the name of the origianl file
$tmp_name $_FILES['uploadfile']['name'];

//    combine to make the newname including path
$new_name $story_path "/" $pre_fix $tmp_name;

//    check to see if file already exists
$alter_name TRUE;
while (
$alter_name) {
 if (
file_exists($new_name)) {
$sub_fix  =  rand(5999);
$new_name $story_path "/" $pre_fix;
$new_name $new_name $sub_fix $tmp_name;
 } else {
$alter_name FALSE;

//    move the file to the appropriate folder
if(move_uploaded_file($_FILES['uploadfile']['tmp_name'], $new_name)) {
} else{
"There was an error uploading the file, please try again!";

//    read the contents of the file
$handle fopen($new_name"r");
$contents fread($handlefilesize($new_name));

//    remove double spaces from the contents
$doubles "  ";
$contents str_replace($doubles," ",$contents);

//    get the first xx words for the teaser
if (count(explode(" "$contents))> $max_words) {
$contents substr($contents0strnpos($contents" "$max_words));
$contents $contents "...";

// check that the flat file exists
// and add the new data to the beginning of the file
$line $pre_fix "|" $new_name "|" $contents "\n";
$flat_file "story.dat";
if (
touch($flat_file)) {
} else {
"Sorry, could not add data";
// send user back to the form page


Finally the script to display the teasers.


this is teaser_display.php

It uses the flat file stories.dat to list the 
teasers as links to the full articles/stories

function make_a_date($new_array) {
$md01 substr($new_array,4,2) . "/" 
$md01 $md01 substr($new_array,6,2) . "/" 
$md01 $md01 substr($new_array,0,4);
date('F d, Y'strtotime($md01));

$flat_file "story.dat";

// read the contents of story.dat into an array
$lines file($flat_file);

// count the number of articles/stories
$count count($lines);

loop thru the array, explode each element into another array
use the elements from this array to populate the teaser list
echo "<table width=650>
            <td width=150 valign=top>
            <td valign=top>

for (
$i=0;$i<$count;$i++) {
$new_array explode("|",$lines[$i]);
$what_date make_a_date($new_array[0]);
$what_path $new_array[1];
$what_teaser $new_array[2];
$new_line '<tr><td valign=top><a href="';
$new_line $new_line  $what_path '">';
$new_line $new_line  $what_date;
$new_line $new_line  '</a></td><td>';
$new_line $new_line  $what_teaser '</td></tr>';