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

Different Strokes - PHP

PHP comes with a powerful and flexible file manipulation API that allows developers (among other things) to read and write files, view and modify file attributes, read and list directory contents, alter file permissions, and retrieve file contents into a variety of native data structures. Find out more, inside.

TABLE OF CONTENTS:
  1. File And Directory Manipulation In PHP (part 1)
  2. Handle With Care
  3. Different Strokes
  4. Weapon Of Choice
  5. Weather Balloon
  6. A Matter Of Existence
  7. Permission Granted
  8. In Stat We Trust
  9. A Short Break
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 74
August 07, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

An alternative way to accomplish the same thing is to use the fgets() function in combination with the feof() function, as demonstrated below:




<?php

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

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

// read file
while (!feof($fh))
{
$data = fgets($fh);
echo $data;
}

// close file
fclose ($fh);

echo "-- ALL DONE --";

?>

The fgets() function works slightly differently from the fread() function, in that it reads a file line by line until the end of the file is reached (the feof() function is used to test for the end-of-file marker).

There are also a couple of other methods of reading data from a file - the very cool file() function, which reads the entire file into an array with one fell swoop, assigning each line as an element of the array. The following example demonstrates:


<?php

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

// read file into array
$data = file($filename) or die("Could not read file!");

// loop through array and print each line
foreach ($data as $line)
{
echo $line;
}

// print file contents
echo "-- ALL DONE --";

?>

As you can see, this example assigns the contents of the file "mindspace.txt" to the array variable $data via the file() function. Each element of the array variable now corresponds to a single line from the file. Once this has been done, it's a simple matter to run through the array and display its contents with the "foreach" loop.

Don't want the data in an array? Try the new file_get_contents() function, which reads the entire file into a string,


<?php

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

// read file into string
$data = file_get_contents($filename) or die("Could not read file!");

// print file contents
echo $data . "-- ALL DONE --";

?>

or use the readfile() function, which reads a file and dumps it directly to the standard output device (in the case of PHP, usually the Web browser):


<?php

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

// read and output file
readfile($filename) or die("Could not read file!");

echo "-- ALL DONE --";

?>



 
 
>>> 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 10 - Follow our Sitemap

Dev Shed Tutorial Topics: