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

    - 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 1 Hosted by Hostway
    Stay green...Green IT