PHP
  Home arrow PHP arrow Page 4 - File And Directory Manipulation In PHP...
CIO Insight
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 
IBM Developerworks
 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

    A high performance database engine using optimized data access for all development environments including Delphi, Visual Studio .NET, Visual Basic, Visual FoxPro. and more. Learn More

    File And Directory Manipulation In PHP (part 1) - Weapon Of Choice
    (Page 4 of 9 )

    If you've used C before, you're probably already familiar with the "include" directive that appears near the beginning of every C program. Since we're talking about reading files, it's appropriate to bring in PHP's equivalent - the include() and require() functions, which come in handy when you need to read an external file into your PHP script.

    Consider the following simple example, which illustrates:


    <html>
    <head>
    <title>Weapons Worth Dying For</title>
    <style>
    h1,h3,li { font-family:Verdana; }
    </style>
    </head>
    <?php

    // this time, Bond's going to need the cigarette-lighter gun... require("./gun.php4");

    // the new car-copter...
    include("./car.php4");

    // and of course, the gold watch with the hidden GPS locator require("./watch.php4");

    ?>

    <body>
    <h3>So, James, here's your check list for the mission.</h3>
    <ol type="a">
    <li>The gun: <?php echo "$gun"; ?>
    <li>The car: <?php echo "$car"; ?>
    <li>The watch: <?php echo "$watch"; ?>
    </ol>
    <br>
    <h3>Oh yes...and remember never to let them see you cry. Good luck, 007!</h3> </body> </html>

    Now, if you try to access this page as it, you'll get a bunch of error messages warning you about missing files. So you need to create the files "gun.php4", "car.php4" and "watch.php4":


    <?php
    // gun.php4
    $gun = "AK-47";
    ?>

    <?php
    // car.php4
    $car = "BMW G8";
    ?>

    <?php
    // watch.php4
    $watch = "Rolex SAW-007";
    ?>

    And this time, when you access the primary page, PHP should automatically include the specified files, read the variables $gun, $watch and $car from them, and display them on the page.

    Files named in the include() and require() functions are searched for in a set of default locations. These locations are specified via PHP's "include_path" configuration directive, which may be set globally in the main "php.ini" configuration file, or on a per-script basis using the
    ini_set() function call.

    A quick note on the difference between the include() and require() functions - the require() function returns a fatal error if the named file cannot be found and halts script processing, while the include() function returns a warning but allows script processing to continue.

    An important point to be noted is that when a file is require()-d or include()-d, the PHP parser leaves "PHP mode" and goes back to regular "HTML mode". Therefore, all PHP code within the included external files needs to be enclosed within regular PHP <?...?> tags.

    A very useful and practical application of the include() function is to use it to include a standard footer or copyright notice across all the pages of your Web site, like this:


    <html>
    <head>
    <title></title>
    </head>

    <body>

    ...your HTML page...

    <br>

    <?php include("footer.html"); ?>

    </body>
    </html>

    where "footer.html" contains


    <font size=-1 face=Arial>This material copyright Melonfire, 2003. All rights reserved.</font>

    Now, this footer will appear on each and every page that contains the
    include() statement above - and, if you need to change the message, you only need to edit the single file named "footer.html"!

    Note also that PHP also offers the require_once() and include_once() functions, which ensure that a file which has already been read is not read again. This can come in handy if you have a situation in which you want to eliminate multiple reads of the same include file, either for performance reasons or to avoid corruption of the variable space.

    {mospagebreak title=A Little Brainwashing}

    Obviously, reading a file is no great shakes - but how about writing to a file? Well, this next script does just that:




    <?php

    // set file to write
    $filename = "matrix.txt";

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

    // write to file
    fwrite($fh, "Welcome to The Matrix, Neo") or die("Could not write to file");

    // close file
    fclose($fh);

    ?>

    Now, once you run this script, it should create a file named "matrix.txt", which contains the text above.

    As you can see, in order to open a file for writing, you simply need to use the same fopen() function, but with a different mode ("w" for write).


    <?php

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

    ?>

    A number of different modes are available for the fopen() function - you've already seen two, here are a few more:


    MODE WHAT IT DOES
    ---------------------------------------------
    r Opens a file in read mode

    w Opens a file in write mode; existing file contents are truncated

    a Opens a file in append mode; existing file contents are preserved

    You can add a "+" to any of the modes above to open the file for simultaneous read and write. In case the named file does not exist in any of the write modes, a new file with the specified name is created.

    Once the file has been opened, you can write text to it simply by specifying the data to be written as an argument to the fwrite() function.


    <?php

    // write to file
    fwrite($fh, "Welcome to The Matrix, Neo") or die("Could not write to file");

    ?>

    Close the file, and you're done!

    You can also write to a file with the new file_put_contents() function, which accepts a string as input and writes it to the named file with minimal fuss or muss.


    <?php

    // set file to write
    $filename = "/tmp/matrix.txt";

    // write to file
    file_put_contents($filename, "Welcome to The Matrix, Neo") or die("Could not write to file");

    ?>

    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 2 hosted by Hostway