PHP
  Home arrow PHP arrow Page 3 - 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 
IBM Developerworks
 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

    Lose your application development headaches. Start developing and deploying applications with Advantage Database Server today. Download a 30-day trial for Free!

    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

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




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