Miscellaneous

  Home arrow Miscellaneous arrow Page 3 - Configuration File Processing with PHP
MISCELLANEOUS

Configuration File Processing with PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 8
    2003-12-13

    Table of Contents:
  • Configuration File Processing with PHP
  • A Simple Configuration File
  • A Grouped Configuration File
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Configuration File Processing with PHP - A Grouped Configuration File


    (Page 3 of 4 )

    A type of configuration file that closely resembles the one we just examined is like that used for the Windows transparency layer for Linux machines, WINE.

    ;; MS-DOS drives configuration
    ;;
    ;; Each section has the following format:
    ;; [Drive X]
    ;; "Path"="xxx"       (Unix path for drive root)
    ;; "Type"="xxx"       (supported types are 'floppy', 'hd', 'cdrom'
    ;;                     and 'network')
    ;; "Label"="xxx"      (drive label, at most 11 characters)
    ;; "Serial"="xxx"     (serial number, 8 characters hexadecimal)
    ;; "Filesystem"="xxx" (supported types are 'msdos'/'dos'/'fat',
    ;;                     'win95'/'vfat', 'unix')
    ;;  This is the FS Wine is supposed to emulate on a certain
    ;;  directory structure.
    ;;

    [Drive C]
    "Path" = "/pack/Work/windows/c"
    "Type" = "hd"
    "Label" = "MS-DOS"
    "Filesystem" = "win95"

    [Drive D]
    "Path" = "/mnt/cdrom"
    "Type" = "cdrom"
    "Label" = "CD-Rom"
    "Filesystem" = "win95"
    ; make sure that device is correct and has proper permissions !

    As we examine the sample from the configuration file we notice that it has been organized into related sections. We also see that the semi-colon is being used to denote comment lines and that we'll also want to treat the double quotes as white space.

    PHP code designed to read such a configuration file might look like this:

    <?php
    $config_file = "my_config.conf";
    $comment = ";";
    $group = "NONE";

    $fp = fopen($config_file, "r");

    while (!feof($fp)) {
      $line = trim(fgets($fp));
      if ($line && !ereg("^$comment", $line)) {
        if (ereg("^\[", $line) && ereg("\]$", $line)) {
          $line = trim($line,"[");
          $line = trim($line, "]");
          $group = trim($line);
        } else {
          $pieces = explode("=", $line);
          $pieces[0] = trim($pieces[0]) , "\"");
          $pieces[1] = trim($pieces[1]) , "\"");
          $option = trim($pieces[0]);
          $value = trim($pieces[1]);
          $config_values[$group][$option] = $value;
        }
      }
    }
    fclose($fp);

    if ($config_values['Drive D']['Type'] == "cdrom")
      echo "Drive D is configured as a CD-Rom device<br />";
    else
      echo "Drive D is not configured as a CD-Rom device<br />";

    Here we've expanded the code from the previous scenario to allow blocked configuration values.

    The $config_file variable still holds the name of the configuration file and $comment has been changed to reflect the use of the semi-colon as a comment delimiter.

    After we obtain a line from the configuration file and have determined it to be something worthwhile, we need to evaluate whether it is a grouping element or a directive. Another comparison to check for an opening and closing brackets at the beginning and end accomplishes this.

    The grouping element is stripped of its brackets and any remaining white space. It's stored as $group and is used as the first branch of our multi-dimensional array so that all related directives can are kept together.

    If the line is a directive then it is split, stripped and stored. The information from the configuration file can be used throughout the script by referencing the array by its group and directive.

    More Miscellaneous Articles
    More By bluephoenix

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap