PHP
  Home arrow PHP arrow Page 4 - Working With Text Files in PHP
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Working With Text Files in PHP
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 78
    2002-12-18

    Table of Contents:
  • Working With Text Files in PHP
  • Opening and Reading Files
  • Creating and Writing Files
  • File Writing in Action
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:

    <html>
    <head>
    <title> Personal Organizer </title>
    </head>
    <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 >>">
    </form>

    </body>
    </html>


    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:

    <?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!");

    @fclose($fp);
    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

    error_reporting(E_NONE);

    ... 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

    More PHP Articles
    More By Mitchell Harper


     

    PHP ARTICLES

    - 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
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT