Programming Basics
  Home arrow Programming Basics arrow Page 8 - Working with text files
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
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? 
PROGRAMMING BASICS

Working with text files
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2004-01-06

    Table of Contents:
  • Working with text files
  • Opening a text file
  • Writing data into a text file
  • Closing a text file
  • Reading from a text file
  • Deleting a file
  • Other functions regarding text files
  • An example
  • Advanced examples
  • 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 - An example


    (Page 8 of 10 )

    Imagine you have to create a mailing list for your site visitors. When we plan this process these are the normal steps which we need to concentrate on:1. Creating a HTML form to input email addresses of visitors. (input.htm)2. PHP page to receive the email address from the HTML page and write them in a text file. (submit.php)3. Read the text file and send an email to all subscribers. (send.php)

    We'll start off the project by stepping on to the first one. Create a simple HTML form with one text field and a submit button.

    input.htm
    <html>
    <head>
    <title>Subcribe</title>
    </head>
    <body>
    <form name="subscribe" action="submit.php" method="post">
    Enter your email address : <input type="text" name="emailadd" /><br />
    <input type="submit" name="submit" value="Subscribe" />
    </form>
    </body>
    </html>

    This will post the email address to the submit.php. What are we waiting for? We can also code it now as you have already learnt all the functions you'll be experiencing on this page.

    submit.php
    <?php
    $emailAddress = $_POST['emailadd'];

    if(!$emailAddress) // If email address is not entered show error message
    {
        echo "Please enter your email address.";
        exit;
    }
    else
    {
        $filename = "subscribers.txt"; // File which holds all data
        $content = "$emailAddress\n"; // Content to write in the data file
        $fp = fopen($filename, "a"); // Open the data file, file points at the end of file
        $fw = fwrite( $fp, $content ); // Write the content on the file
        fclose( $fp ); // Close the file after writing
        
        if(!$fw) echo "Couldn't write the entry.";
        else echo "Successfully wrote to the file.";
    }
    ?>

    That's it. I hope you could understand the process with the help of comments written inside the code. This code simply checks whether user has entered an email address. Then if he hasn't entered, it shows an error message, or if he has entered an address, it writes it into the text file where all the addresses are stored.

    The next part of the project is retrieving all email addresses in the file and sending emails to all of them. Let's go through that part and sum up the project now.

    send.php
    <?php
    $filename = "subscribers.txt"; // File which holds all data
    $arrFp = file( $filename ); // Make array of file content lines
    $numAdds = count( $arrFp ); // Count no. of elements in the array, count email addresses

    for($i=0; $i<$numAdds; $i++)
    {
        $emailAddress = trim( $arrFp[$i] ); // Trim email addresses before sending mails
        echo "Sending email to $emailAddress...";
        $success = mail( $emailAddress, "Test email", "This is a test email message");
        
        if($success) echo "Success<br>";
        else echo "Error<br>";
    }
    ?>

    As I told you in a prior chapter, here we have an example of the file() function. In the second line of this code, there is a new array getting created called, $arrFp which holds an email address as each element. Then you can basically loop through the array and send mails to everyone.

    You're done! Now it's all yours!

    More Programming Basics Articles
    More By Codewalkers


       · Well laid steps in this tutorial. Thank you.
       · You have done a great job here.Thanks
       · no crap... good tutorial
       · This is the text.
       · Thanks, a very helpful tutorial
       · Really, I built a whole portal engine in 4 days with this tutorial. You can check it...
       · Your guide is very interesting, useful and good.Can I translate it in Romanian...
     

    PROGRAMMING BASICS ARTICLES

    - PHP: Hypertext Preprocessor: What is it?
    - Loops and PHP Decision Making
    - Operators, Conditionals, and PHP Decision-Ma...
    - PHP Decision-Making
    - Coding
    - Server Statistics
    - Looping in PHP
    - Cookies in PHP
    - Working with text files
    - Beginning Object Oriented Programming in PHP
    - A Tour of Decision Making Structures in PHP
    - PHP Strings Primer
    - PHP Control Structures
    - Intro to Vim
    - Reading Directorys with PHP





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek