Class 7 – Working With Comma-Delimited Text Files

Posted: April 4th, 2009 | Author: | Filed under: php | Tags: | No Comments »

As a hint for your advanced assignment, here is an example of working with comma-delimited text files.  I have taken the example of a text file that has three fields for each line: image_path, caption, and username.  So the text file is structured something like this:

files/image1.jpg,this is the caption for the first image,amos
files/image2.jpg,this is the caption for the second image,amos
files/image3.jpg,this is the caption for the third image,amos

Where each line of the text file has those three fields separated by commas.

The file, index.php, compiles an array that contains the data for each line of the text file.  It then loops through this array and outputs each field of each line of text into an XHTML table tag so you can see it on the page.

The add_image.php, and process_add_image.php scripts write new data to the text file.  They assemble a line of text with three fields broken up by commas, and then write that line to the text file.  Once the line has been written, the user is redirected to the index.php page.

No related posts.



Leave a Reply