Home arrow PHP arrow Page 3 - File And Directory Manipulation In PHP (part 2)

Fertile Fields - PHP

Now that you know the basics of reading and writing files, this second segment of our tutorial on the PHP filesystem API takes you into deeper waters, showing you how to copy, delete and rename files; scan directories; work with uploaded files over HTTP; perform pattern matches on file names; and read and write to processes instead of files.

TABLE OF CONTENTS:
  1. File And Directory Manipulation In PHP (part 2)
  2. Stripping It To The Bone
  3. Fertile Fields
  4. Configuring The System
  5. The Right Path
  6. Move It
  7. Beam Me Up
  8. Diving Into Directories
  9. A Pattern Emerges
  10. Purging The Dead
  11. Size Does Matter
  12. In Process
  13. Disk Full
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 65
August 21, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


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

Dev Shed Tutorial Topics: