PHP
  Home arrow PHP arrow Page 3 - File And Directory Manipulation In PHP...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
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

File And Directory Manipulation In PHP (part 1)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 64
    2003-08-07

    Table of Contents:
  • File And Directory Manipulation In PHP (part 1)
  • Handle With Care
  • Different Strokes
  • Weapon Of Choice
  • Weather Balloon
  • A Matter Of Existence
  • Permission Granted
  • In Stat We Trust
  • A Short Break

  • 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

    Application developers can seamlessly integrate the Advantage Database install with their application install. Learn the best practices used when setting up silent installs with this seminar.

    File And Directory Manipulation In PHP (part 1) - Different Strokes
    (Page 3 of 9 )

    An alternative way to accomplish the same thing is to use the fgets() function in combination with the feof() function, as demonstrated below:




    <?php

    // set file to read
    $filename = "mindspace.txt";

    // open file
    $fh = fopen ($filename, "r") or die("Could not open file");

    // read file
    while (!feof($fh))
    {
    $data = fgets($fh);
    echo $data;
    }

    // close file
    fclose ($fh);

    echo "-- ALL DONE --";

    ?>

    The fgets() function works slightly differently from the fread() function, in that it reads a file line by line until the end of the file is reached (the feof() function is used to test for the end-of-file marker).

    There are also a couple of other methods of reading data from a file - the very cool file() function, which reads the entire file into an array with one fell swoop, assigning each line as an element of the array. The following example demonstrates:


    <?php

    // set file to read
    $filename = "mindspace.txt";

    // read file into array
    $data = file($filename) or die("Could not read file!");

    // loop through array and print each line
    foreach ($data as $line)
    {
    echo $line;
    }

    // print file contents
    echo "-- ALL DONE --";

    ?>

    As you can see, this example assigns the contents of the file "mindspace.txt" to the array variable $data via the file() function. Each element of the array variable now corresponds to a single line from the file. Once this has been done, it's a simple matter to run through the array and display its contents with the "foreach" loop.

    Don't want the data in an array? Try the new file_get_contents() function, which reads the entire file into a string,


    <?php

    // set file to read
    $filename = "mindspace.txt";

    // read file into string
    $data = file_get_contents($filename) or die("Could not read file!");

    // print file contents
    echo $data . "-- ALL DONE --";

    ?>

    or use the readfile() function, which reads a file and dumps it directly to the standard output device (in the case of PHP, usually the Web browser):


    <?php

    // set file to read
    $filename = "mindspace.txt";

    // read and output file
    readfile($filename) or die("Could not read file!");

    echo "-- ALL DONE --";

    ?>

    More PHP Articles
    More By icarus, (c) Melonfire


     

       

    PHP ARTICLES

    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5
    - Working with Multiple Document Nodes with th...




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