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

Handle With Care - 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

I'll begin with something simple - opening a file and reading its contents. Let's assume that we have a text file called "mindspace.txt", containing the following random thoughts:




We're running out of space on planet Earth.
Scientists are attempting to colonize Mars.
I have a huge amount of empty real estate in my mind.
Imagine if I could rent it to the citizens of Earth for a nominal monthly fee.
Would I be rich? Or just crazy?

Now, in order to read this data into a PHP script, I need to open this file and assign it a file handle - I can then use this file handle to interact with the file and extract its contents into a PHP variable. Take a look:


<?php

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

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

// read file contents
$data = fread($fh, filesize($filename)) or die("Could not read file!");

// close file
fclose($fh);

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

?>

And when you run this script, PHP should return the contents of the file "mindspace.txt", with a message at the end.

A quick explanation: in order to read data from an external file, PHP requires you to define a file handle for it with the fopen() function. I've done this in the very first line of the script above.


<?php

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

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

?>

You can specify a full path to the file as well:


<?php

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

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

?>

Notice the second argument to fopen() - it's a string indicating the "mode" in which the file is to be opened. A number of modes are available - read, write, append and so on - and I'll discuss them in detail a little further along.

If the fopen() function is successful, it returns a file handle (stored in
$fh) which can be used for further interaction with the file. This file handle is provided to the fread() function, which uses it to read the contents of the file.


<?php

// read file contents
$data = fread($fh, filesize($filename)) or die("Could not read file!");

?>

The second argument to fread() is the number of bytes to be read. In this case, I've used the filesize() function to obtain the size of the file in bytes and provide that number to the fread() function; you can set an arbitrary value here if you like, but be warned that reading will automatically stop once the end of the file is reached.

Once the file contents have been read into a variable with fread(), the
fclose() function is used to close the file and destroy the file handle created by fopen(). This is not strictly necessary, but it is a good habit to develop as it avoids loose ends cluttering up your script.


<?php

// close file
fclose($fh);

?>



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

 


Dev Shed Tutorial Topics: