Logo         Community
The Company
Your Account
Contact Us
PHP 101 (part 5): Rank And File
Learn how to read and write files with PHP.

| Handle With Care |

I'll begin with something simple: opening a file and reading its contents. Let's assume that somewhere on your disk, hidden under /usr/local/stuff/that/should/be/elsewhere/recipes/, you have a text file containing the recipe for the perfect Spanish omelette. You now wish to read the contents of this file into a PHP script.

In order to do this, there are three distinct steps to be followed:

1. Open the file and assign it a file handle.

2. Interact with the file, via its handle, and extract its contents into a PHP variable.

3. Close the file.

Here's a PHP script that does just that:


// set file to read
$file = '/usr/local/stuff/that/should/be/elsewhere/recipes/omelette.txt';
// open file
$fh = fopen($file, 'r') or die('Could not open file!');
// read file contents
$data = fread($fh, filesize($file)) or die('Could not read file!');
// close file
// print file contents
echo $data;


Run this script through your Web browser, and PHP should return the contents of the file.

Now let me explain each of the three steps above in detail:

1. Open the file and assign it a file handle
PHP needs a file handle to read data from a file. This file handle can be created with the fopen() function, which accepts two arguments: the name and path to the file, and a string indicating the "mode" in which the file is to be opened ('r' for read).

Three different modes are available for use with the fopen() function. Here's the list:

'r' - opens a file in read mode

'w' - opens a file in write mode, destroying existing file contents

'a' - opens a file in append mode, preserving existing file contents

2. Interact with the file via its handle and extract its contents into a PHP variable
If the fopen() function is successful, it returns a file handle, $fh, which can be used for further interaction with the file. This file handle is used by the fread() function, which reads the file and places its contents into a variable.

The second argument to fread() is the number of bytes to be read. You can usually obtain this information through the filesize() function, which - who'd have guessed it?!- returns the size of the file in bytes.

3. Close the file
This last step is not strictly necessary as PHP closes the file automatically once it reaches the end of the script, but it's a good habit to develop. Explicitly closing the file with fclose() has two advantages: it ties up loose ends in your script, and it wins you lots of good karma from the PHP community.

You probably haven't see the die() function before, either. This function is mostly used as a primitive error-handling mechanism. In the event of a fatal error, such as the file path being invalid or the file permissions being such that PHP cannot read it, die() terminates script processing and optionally displays a user-specified error message indicating why it committed suicide.

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback