PHP
  Home arrow PHP arrow Page 4 - Reading, Writing and Creating Files in PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Reading, Writing and Creating Files in PHP
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 34
    2006-08-23


    Table of Contents:
  • Reading, Writing and Creating Files in PHP
  • File Permissions in a Unix Environment
  • Writing to Files
  • Reading from Files

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Reading, Writing and Creating Files in PHP - Reading from Files
    ( Page 4 of 4 )

    In PHP, to read from a file is easier than to write to a file. Instead of creating a file pointer and using the fopen() function, you simply read the entire file into an array and then retrieve it from there:

    $theArray = file('theFilename');

    The file() function does all the work of reading the entire file and placing it in a array. Each element in the array will then contain one line from the file.

    To demonstrate how to read from a file, let's read the names from the text file that we stored them in. Create a PHP document and call it readnames.php:

     <?
    $thenames = file('names.txt');
    $num_names = count($thenames);
    for($i=0; $i < $num_names; $i++){
    echo $thenames[$i]. "<br>";
    }
    ?>

    The for Loop runs through the contents of the arrays and prints the names out. Below is a screen shot of the results I got:

    Conclusion

    We're finished with part one of this discussion. In part two of this article, we will be looking at file uploading and how to handle file uploads. Till then, have fun reading and writing from files.



     
     
    >>> More PHP Articles          >>> More By Jacques Noah
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





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