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

    - 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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek