Programming Basics

  Home arrow Programming Basics arrow Page 7 - 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 - Other functions regarding text files


    (Page 7 of 10 )

    There are a few other functions which we did not talk about in previous chapters. We can learn about them one by one in this chapter.

    fgets()

    In case you want to read a file one line at a time, this is what you need.

    <?php 
    $buffer = fgets( $fp, 100); 
    ?>

    In this case, this script will read until it finds a new line character (\n), encounters an EOF, or has read 99 bytes from the file. The maximum length read is the length specified minus one byte. The fgets() function is useful when dealing with files that contain plain text that we want to deal with in chunks.

    http://us2.php.net/manual/en/function.fgets.php

    feof()

    This function takes file pointer as its single parameter. It will return true if the file pointer is end of file and false if it is somewhere else in the file. We commonly use this function when looping through a file until the end of file.

    <?php
    while (!feof( $fp ))
    ?>

    http://us2.php.net/manual/en/function.feof.php

    An example:

    <?php
    $fp = fopen ("notes/data/names.txt", "r"); 
    while (!feof ($fp)) { 
        $content = fgets( $fp, 4096 ); 
        echo $content; 

    fclose ($fp); 
    ?>

    The above example simply outputs the content in the text file which is opened in the first line of the script. This uses the feof() function which is mentioned above and it read the file until EOF file occurs. While running the loop it echos the content read line by line fgets() function!

    readfile()

    Instead of reading line by line, we can read the whole file in one go with readfile(). Secondly this automatically echos the contents of the file to the browser without adding echo() or print(). Unlike other functions this takes the parameter of filename with the path of it, not a file pointer.

    <?
    readfile( "notes/data/names.txt" );
    ?>

    http://us2.php.net/manual/en/function.readfile.php

    file()

    This is a very important function when it comes to file reading. It also identical to the readfile() except that instead of echoing the file as an output, it turns it into an array. Elements of the array will be the lines of the opened file. We'll go through this function with more details in examples at the end of the tutorial.

    <?php
    $arrContent = file( "notes/data/names.txt" );
    ?>

    http://us2.php.net/manual/en/function.file.php

    file_exists()

    Just to check whether given file exists, simply use it like this.

    <?php
    $filename = "notes/data/names.txt";
    if(file_exists( $filename ))
        echo "The file $filename exists.";
    else
        echo "The file $filename does not exists.";
    ?>

    http://us2.php.net/manual/en/function.file-exists.php

    filesize()

    I hope you can remember we used this function when we were reading the file with fread(). You can check the size of a file in bytes like this.

    <?php
    $filename = "notes/data/names.txt";
    echo "$filename is ". filesize( $filename ). " bytes big.";
    ?>

    http://us2.php.net/manual/en/function.filesize.php

    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 6 - Follow our Sitemap