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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    File And Directory Manipulation In PHP (part 2) - Configuring The System
    (Page 4 of 13 )

    If what you're really after involves reading configuration variables in from a standard .INI file, you don't need to write custom code to parse the file and read in the variable-value pairs. Instead, just use PHP's
    parse_ini_file() function, which automatically takes care of this for you.

    Consider the following sample .INI file,


    [global]
    printing = bsd
    default case = lower
    log file = /var/log/samba/log.%m
    printcap name = /etc/printcap
    max log size = 50
    domain master = yes
    dns proxy = no

    [temp]
    comment = Temporary file space
    path = /tmp
    read only = no
    public = yes
    create mask = 0777
    force group = nobody
    force user = nobody

    and the PHP code to parse it:


    <?php

    // set file to read
    $filename = "samba.ini";

    // read INI file into array
    $data = parse_ini_file($filename);

    // print array
    print_r($data);

    ?>

    A quick glance at the output shows that PHP has, indeed, read the file, parsed its contents, and converted the variable-value pairs into an associative array.


    Array
    (
    [printing] => bsd
    [default case] => lower
    [log file] => /var/log/samba/log.%m
    [printcap name] => /etc/printcap
    [max log size] => 50
    [domain master] => 1
    [dns proxy] =>
    [comment] => Temporary file space
    [path] => /tmp
    [read only] =>
    [public] => 1
    [create mask] => 0777
    [force group] => nobody
    [force user] => nobody
    )

    The only problem with the approach, however, is that variables with the same name from different sections will override each other; if there are multiple configuration variables with the same name, the output array will always contain only the last value. In order to illustrate, look what happens when I add a new section to the sample file above which repeats some of the variables from a previous section:


    [global]
    printing = bsd
    default case = lower
    log file = /var/log/samba/log.%m
    printcap name = /etc/printcap
    max log size = 50
    domain master = yes
    dns proxy = no

    [temp]
    comment = Temporary file space
    path = /tmp
    read only = no
    public = yes
    create mask = 0777
    force group = nobody
    force user = nobody

    [shared]
    comment = Shared area
    path = /shared
    printable = no
    create mode = 0770
    directory mode = 0770
    writeable = yes

    Here's the output:


    Array
    (
    [printing] => bsd
    [default case] => lower
    [log file] => /var/log/samba/log.%m
    [printcap name] => /etc/printcap
    [max log size] => 50
    [domain master] => 1
    [dns proxy] =>
    [comment] => Shared area
    [path] => /shared
    [read only] =>
    [public] => 1
    [create mask] => 0777
    [force group] => nobody
    [force user] => nobody
    [printable] =>
    [create mode] => 0770
    [directory mode] => 0770
    [writeable] => 1
    )

    As you can see, some of the variable-value pairs (from the "temp" section of the file) have been lost. PHP offers a solution to this problem by allowing a second, optional argument to parse_ini_file() - a Boolean indicating whether the namespaces of the various sections should be respected. When I add that to the script above,


    <?php

    // set file to read
    $filename = "samba.ini";

    // read INI file into array
    // process each section separately
    $data = parse_ini_file($filename, true);

    // print array
    print_r($data);

    ?>

    look how drastically the output changes:


    Array
    (
    [global] => Array
    (
    [printing] => bsd
    [default case] => lower
    [log file] => /var/log/samba/log.%m
    [printcap name] => /etc/printcap
    [max log size] => 50
    [domain master] => 1
    [dns proxy] =>
    )

    [temp] => Array
    (
    [comment] => Temporary file space
    [path] => /tmp
    [read only] =>
    [public] => 1
    [create mask] => 0777
    [force group] => nobody
    [force user] => nobody
    )

    [shared] => Array
    (
    [comment] => Shared area
    [path] => /shared
    [printable] =>
    [create mode] => 0770
    [directory mode] => 0770
    [writeable] => 1
    )

    )

    With the addition of the second argument to parse_ini_file(), PHP now creates a nested array, with the outer array referencing the sections, and each inner one referencing the variables in each section.

    More PHP Articles
    More By icarus, (c) Melonfire


     

       

    PHP ARTICLES

    - Setting Up a Web-based Image Hosting Service
    - 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

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