PHP
  Home arrow PHP arrow Page 10 - 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? 
Google.com  
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) - Purging The Dead
    ( Page 10 of 13 )

    Not only does PHP let you read directory contents, it also allows you to create and delete directories with the mkdir() and rmdir() functions respectively. Consider the following example, which creates a directory,


    <?php

    // create directory
    mkdir("/tmp/stuff") or die ("Could not make directory");

    ?>

    and its mirror image, which removes the newly-created directory.


    <?php

    // erase directory
    rmdir("/tmp/stuff") or die ("Could not erase directory");

    ?>

    As with the other filesystem manipulation functions, these functions too will fail if the user the Web server is running as lacks sufficient privileges to create and delete directories on the disk. Directories created with mkdir() will be owned by the process running the Web server.

    It's interesting also to note that the rmdir() function operates only if the directory to be deleted is completely empty. Try running it on a directory containing existing files, as below,


    <?php

    // create directory
    mkdir("/tmp/stuff") or die ("Could not make directory");

    // create sub-directory
    mkdir("/tmp/stuff/personal") or die ("Could not make directory");

    // remove parent directory
    rmdir("/tmp/stuff") or die ("Could not remove directory");

    ?>

    and you'll be rewarded with the following error:


    Warning: mkdir(): File exists in /home/web/rmdir.php on line 4 Could not remove directory

    Since it's unlikely that you'll find empty directories just waiting for your rmdir() in the real world, you'll normally need to empty the directory manually prior to calling rmdir() on it. The following example demonstrates, by combining the unlink() function discussed previously with some recursive logic to create a function designed specifically to erase the contents of a directory (and its sub-directories) in one fell swoop:


    <?php

    // recurses into a directory and empties it
    // call it like this: purge("/dir/")
    // remember the trailing slash!
    function purge($dir)
    {
    $handle = opendir($dir);
    while (false !== ($file = readdir($handle)))
    {
    if ($file != "." && $file != "..")
    {
    if (is_dir($dir.$file))
    {
    purge ($dir.$file."/");
    rmdir($dir.$file);
    }
    else
    {
    unlink($dir.$file);
    }
    }
    }
    closedir($handle);
    }

    ?>



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5





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