Programming Basics

  Home arrow Programming Basics arrow Page 2 - Working with text files
PROGRAMMING BASICS

Working with text files
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Working with text files - Opening a text file


    (Page 2 of 10 )

    The first thing to do is to open a text file in your web directory. To open a text file, the file should exist in the given path. In the file you are trying to open can't be found, you can have PHP to create a new one for you.

    To open a file in PHP, we simply use fopen() function. At the point we open the selected file, we need to specify how we intend to use it after opening. So, that means before opening a file using PHP's fopen(), we need to plan for what type of purposes we hope to use the opened file. This is called "File mode".

    File Modes :* Opening a file for reading only, for writing only or for both.* When you write to the file, you might want to overwrite current data in it or write new data without overwriting existing.

    fopen() function

    When fopen() is called, there are two parameters. The first parameter should be the file you want to open. You can specify a directory path to this file as shown below:

    <?php
    $fp = fopen( "notes/data/names.txt" , "w" );
    ?>

    The second parameter of this function is the file mode. This parameter indicates what you are planning to do with the opened file. In above case we are using "w", so that means open the file for writing.

    For more information about file modes : http://us2.php.net/manual/en/function.fopen.php

    If fopen() opens the file successfully, a pointer to the file is returned and will be stored in a variable, in above case $fp. You must use that variable whenever you want to work with the opened file.

    When an error occurs while calling fopen() the function returns false. You could handle this error situation in a user-friendly way.

    <?php
    $fp = fopen( "notes/data/names.txt", "w" );

    if(!$fp)
    {
        echo "Couldn't open the data file. Try again later.";
        exit;
    }
    ?>

    More Programming Basics Articles
    More By Codewalkers

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP
    - Calendar Construction with PHP
    - PHP`s Calendar Package
    - Getting Modified Versions and Correct Dates ...
    - Combining Date Functions in PHP
    - Using PHP for Date and Time in Programming
    - More Exception Handling with PHP
    - Exception Handling in PHP
    - Error Logging and Handling Exceptions
    - Configuration Directives for Error and Excep...
    - Error and Exception Handling
    - Python Modules for Games


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap