Programming Basics
  Home arrow Programming Basics arrow Page 7 - 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 - 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


       · 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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek