PHP
  Home arrow PHP arrow Page 5 - 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

    Create a Highly Available Database Solution. Advantage Database Server can provide the availability to keep your data safe and ready for your application and users.

    File And Directory Manipulation In PHP (part 2) - The Right Path
    (Page 5 of 13 )

    In addition to functions that allow you to obtain information on file sizes, permissions and modification times, PHP also offers a number of functions designed to manipulate file and path names, and split a file path into its constituent components. The two most commonly-used ones here are the basename() function, which returns the filename component of a path, and the dirname() function, which returns the directory name component of a path

    The following example demonstrates the basename() and dirname() components in action, by splitting a file path into its constituents:


    <?php

    // set path
    $path = "/usr/local/apache/bin/httpd";

    // print filename
    echo "File is " . basename($path) . "\r\n";

    // print directory name
    echo "Directory is " . dirname($path) . "\r\n";

    ?>

    Here's the output:


    File is httpd
    Directory is /usr/local/apache/bin

    You can also use the pathinfo() function to obtain this information - this function returns an associative array containing keys for directory name, file name and file extension. Take a look at this next script, which returns this information for the directory holding the currently executing script.


    <?php

    // parse current file path
    $info = pathinfo($_SERVER['PHP_SELF']);

    // print info
    print_r($info);

    ?>

    Here's the output:


    Array
    (
    [dirname] => /dev/php
    [basename] => fs.php
    [extension] => php
    )

    Finally, you can use the realpath() function to translate relative paths into absolute ones, as below:


    <?php

    // returns /usr/local/lib
    echo realpath("/usr/local/apache/bin/../../lib/");

    // returns /lib
    echo realpath("../../lib");

    ?>

    You can obtain the script's current working directory by combining the
    dirname() function with the special $_SERVER['SCRIPT_FILENAME'] variable,


    <?php

    echo "Current directory is " . dirname($_SERVER['SCRIPT_FILENAME']);

    ?>

    or with the alternative getcwd() function, as below,


    <?php

    // get current working directory
    echo "Current directory is " . getcwd();

    ?>

    or even through creative use of the realpath() function, as below:


    <?php

    echo "Current directory is " . realpath(".");

    ?>

    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...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway