PHP
  Home arrow PHP arrow Page 2 - File And Directory Manipulation In PHP (part 1)
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? 
PHP

File And Directory Manipulation In PHP (part 1)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 69
    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:
      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


    File And Directory Manipulation In PHP (part 1) - Handle With Care
    ( Page 2 of 9 )

    I'll begin with something simple - opening a file and reading its contents. Let's assume that we have a text file called "mindspace.txt", containing the following random thoughts:




    We're running out of space on planet Earth.
    Scientists are attempting to colonize Mars.
    I have a huge amount of empty real estate in my mind.
    Imagine if I could rent it to the citizens of Earth for a nominal monthly fee.
    Would I be rich? Or just crazy?

    Now, in order to read this data into a PHP script, I need to open this file and assign it a file handle - I can then use this file handle to interact with the file and extract its contents into a PHP variable. Take a look:


    <?php

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

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

    // read file contents
    $data = fread($fh, filesize($filename)) or die("Could not read file!");

    // close file
    fclose($fh);

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

    ?>

    And when you run this script, PHP should return the contents of the file "mindspace.txt", with a message at the end.

    A quick explanation: in order to read data from an external file, PHP requires you to define a file handle for it with the fopen() function. I've done this in the very first line of the script above.


    <?php

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

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

    ?>

    You can specify a full path to the file as well:


    <?php

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

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

    ?>

    Notice the second argument to fopen() - it's a string indicating the "mode" in which the file is to be opened. A number of modes are available - read, write, append and so on - and I'll discuss them in detail a little further along.

    If the fopen() function is successful, it returns a file handle (stored in
    $fh) which can be used for further interaction with the file. This file handle is provided to the fread() function, which uses it to read the contents of the file.


    <?php

    // read file contents
    $data = fread($fh, filesize($filename)) or die("Could not read file!");

    ?>

    The second argument to fread() is the number of bytes to be read. In this case, I've used the filesize() function to obtain the size of the file in bytes and provide that number to the fread() function; you can set an arbitrary value here if you like, but be warned that reading will automatically stop once the end of the file is reached.

    Once the file contents have been read into a variable with fread(), the
    fclose() function is used to close the file and destroy the file handle created by fopen(). This is not strictly necessary, but it is a good habit to develop as it avoids loose ends cluttering up your script.


    <?php

    // close file
    fclose($fh);

    ?>



     
     
    >>> More PHP Articles          >>> More By icarus, (c) Melonfire
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT