Home arrow PHP arrow Page 4 - Working With Text Files in PHP

Working With Text Files in PHP

Sure, you know PHP's MySQL functions, but do you know how to read/write files in PHP? If not, then make sure you read this article. In it, Mitch teaches you everything you need to know to successfully work with files in PHP...

Author Info:
By: Mitchell Harper
Rating: 5 stars5 stars5 stars5 stars5 stars / 103
December 18, 2002
  1. · Working With Text Files in PHP
  2. · Opening and Reading Files
  3. · Creating and Writing Files
  4. · File Writing in Action
  5. · Conclusion

print this article

Working With Text Files in PHP - File Writing in Action
(Page 4 of 5 )

Let's create an example that will grab the name, sex and age of someone from a HTML form and write them to a file. There will be 2 pages in our example: getdata.php and savedata.php.

Here's the code for getdata.php:

<title> Personal Organizer </title>
<body bgcolor="#ffffff">

<form action="savedata.php" method="post">
First Name: <input type="text" name="first_name" size="30"><br>
Last Name: <input type="text" name="last_name" size="30"><br>
Age: <input type="text" name="age" size="2"><br>
Sex: <input type="radio" name="sex" value="M"> Male <input type="radio" name="sex"

value="F"> Female<br><br>
<input type="submit" value="Save Data >>">


Nothing fancy here. Just a simple HTML form that posts data to a file called savedata.php. The output from the HTML above looks like this:

The output from getdata.php

The savedata.php file will grab these values and save them into a file called organizer.data. Note that I will not be implementing any error checking on the form values -- only on the file operations.

Here's savedata.php:


$fName = @$_POST["first_name"];
$lName = @$_POST["last_name"];
$age = is_numeric(@$_POST["age"]) ? $_POST["age"] : 0;
$sex = @$_POST["sex"] == "" ? "M" : $_POST["sex"];

// Set the string to be written to the file
$values = "Name: $fName $lName\r\n";
$values .= "Age: $age\r\n";
$values .= "Sex: $sex\r\n";

// Open the file for truncated writing
$fp = @fopen("organizer.data", "w") or die("Couldn't open organizer.data for writing!");
$numBytes = @fwrite($fp, $values) or die("Couldn't write values to file!");

echo "Wrote $numBytes bytes to organizer.data successfully!";


As you can see, I've used our fopen, fwrite and fclose functions with some @ symbols to stop PHP spitting any errors if they occur. If errors do occur, then the die() function will be called and a message output before the script terminates. Alternatively, you could've added


... to the top of the file to stop any type of error being displayed. Our file, organizer.data now looks like this:

Our organizer.data file
blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials