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

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 



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

Dev Shed Tutorial Topics: