PHP
  Home arrow PHP arrow Page 6 - 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 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 59
    2003-08-21

    Table of Contents:
  • File And Directory Manipulation In PHP (part 2)
  • Stripping It To The Bone
  • Fertile Fields
  • Configuring The System
  • The Right Path
  • Move It
  • Beam Me Up
  • Diving Into Directories
  • A Pattern Emerges
  • Purging The Dead
  • Size Does Matter
  • In Process
  • Disk Full

  • 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

    Minimize the cost of deploying database applications. Advantage Database Server or Microsoft SQL Server – Which One is Right for You? Learn now!

    File And Directory Manipulation In PHP (part 2) - Move It
    (Page 6 of 13 )

    In addition to offering you path information, PHP comes with a whole bunch of functions designed to simplify the task of moving, copying, renaming and deleting files on the filesystem. The first of these is the copy() function, which accepts two arguments, a source file and a destination, and copies the former to the latter. Here's an example which demonstrates:


    <?php

    // check to see if file exists
    // if so, back it up
    if (file_exists("matrix.txt"))
    {
    copy ("matrix.txt", "matrix.txt.orig") or die ("Could not copy file"); }

    ?>

    Note that if the destination file already exists, copy() will usually overwrite it. A failure to copy the file will cause copy() to return false; success returns true.

    A corollary to the copy() function is the rename() function, which can be used to both rename and move files. Like copy(), it too accepts two arguments, a source file and a destination file. Consider the following example, which renames a file,


    <?php

    // check to see if file exists
    // if so, rename it
    if (file_exists("matrix.txt"))
    {
    rename ("matrix.txt", "neoworld.txt") or die ("Could not rename file"); }

    ?>

    and this one, which simultaneously renames and moves a file.


    <?php

    // check to see if file exists
    // if so, move and rename it
    if (file_exists("/home/john/newsletters"))
    {
    rename ("/home/john/newsletters", "/home/john/mail/lists") or die ("Could not move file"); }

    ?>

    It's possible to rename directories in the same manner as files - as illustrated in this next snippet:


    <?php

    // check to see if directory exists
    // if so, rename it
    if (is_dir("Mail"))
    {
    rename ("Mail", "mail-jun-03") or die ("Could not rename directory"); }

    ?>

    The rename() function comes in handy when you need to update files which are constantly being used by multiple processes. Instead of directly updating the target file with new data, rename() allows you to copy the original file contents into a new file, make your changes and then, once you're happy with the result, simply rename() the new file to the old one.

    The following example demonstrates:


    <?php

    // read file
    $contents = file("/etc/inittab") or die ("Could not read file");

    // add a line to it
    $contents[] = "x:5:respawn:/etc/X11/prefdm -nodaemon";

    // write contents to temporary file
    $tmpfile = tempnam("/tmp", "inittab.tmp.");

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

    foreach ($contents as $line)
    {
    fwrite ($fh, $line) or die ("Could not write file");
    }

    // close file
    fclose ($fh);

    // move temporary file over original
    rename($tmpfile, "/etc/inittab") or die ("Could not replace file");

    ?>

    Note my use of the tempnam() function above - this function generates a unique file name, given a directory and a filename prefix, and can help to avoid filename collisions between different processes.

    When it comes time to delete files, PHP offers the unlink() function, which can be used to erase a file from the filesystem. Consider the following example, which demonstrates by deleting a specific file:


    <?php

    // check to see if file exists
    // if so, erase it
    if (file_exists("error.log"))
    {
    unlink ("error.log") or die ("Could not delete file");
    }

    ?>

    You can also use the unlink() function to iterate over a directory and remove all the files within it - this is demonstrated in an example coming up shortly.

    Note that the unlink() function (and indeed, all other file manipulation
    functions) will fail if the user ID under which the Web server is running does not have adequate permissions to delete or otherwise modify the named file(s).

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