PHP
  Home arrow PHP arrow Page 3 - 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? 
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) - Fertile Fields
    ( Page 3 of 13 )

    In addition to the simple fgets() function, PHP also offers the more-sophisticated fgetcsv() function, which not only reads in data from a file, but also parses each line and, using the comma (,) symbol as delimiter, splits the data on each line into fields for further processing. The return value from every call to fgetcsv() is an array containing the fields found.

    An example might make this clearer. Consider the following CSV file,


    john,67,John Doe,India
    sue,32,Sue Me,New York
    sarah,10,Sarah Whu,Korea
    ramu,23,R Amulet,London

    and this PHP script, which reads it and displays the information within it as an XML document:


    <?php

    // open XML tags
    echo "<?xml version='1.0'?>";
    echo "<collection>";

    // set file to read
    $filename = "users.txt";

    // open file
    $fh = fopen ($filename, "r") or die("Could not open file");

    // read file
    while (!feof($fh))
    {
    // create XML structure
    echo "<user>";
    $fields = fgetcsv($fh, 1000);
    echo "<username>" . $fields[0] . "</username>";
    echo "<fullname>" . $fields[2] . "</fullname>";
    echo "<age>" . $fields[1] . "</age>";
    echo "<city>" . $fields[3] . "</city>";
    echo "</user>";
    }

    // close file
    fclose ($fh);

    echo "</collection>";

    ?>

    In this case, the comma-separated values in the input file are automatically parsed into an array, and can then be processed, or reassembled in any order you like, to create different output. Here's what the script above results in:


    <?xml version='1.0'?> <collection><user><username>john</username><fullname> John
    Doe</fullname><age>67</age><city>India</city> </user><user><username>sue</use
    rname><fullname>Sue Me</fullname><age>32</age><city>New
    York</city> </user><user><username>sarah</username><fullname> Sarah
    Whu</fullname><age>10</age><city>Korea</city> </user><user><username>ramu</us
    ername><fullname> R
    Amulet</fullname><age>23</age><city>London</city> </user></collection>



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

       

    PHP ARTICLES

    - 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
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT