PHP
PHP introduction
PHP basics
PHP variables
PHP functions
PHP conditions
PHP loops
PHP arrays
PHP OOP
PHP strings
PHP forms
PHP entitites
PHP files
PHP include files
PHP date and time
PHP cookies
PHP databases
PHP sessions
PHP summary

Programming

Programming intro
Java

Markup

First webpage guide
HTML
XHTML

Browser scripting

Javascript
VBScript
AJAX

Server scripting

ASP

Making money online

Make money online

PHP file input/output

PHP provides various functions for working with files.

This lesson focuses on:

Opening files

The function used to open files is fopen(). This function takes two parameters - the file name, and a file access modifier.

Syntax:

fopen("filename", "access modifier");

The filename is the file to be opened. The access modifier denotes what the file can be used for.

File access modifiers:

Example:

<?php

fopen("fileOne.txt", "a");

?>

In the above example, a file named fileOne.txt is opened with append access. Any data written to it will be appended without erasing previous data.

Creating file handles

Once you open a file, the fopen() function return an integer called a file handle. This integer will refer to the file in other functions. Storing the file handle in a variable is important so that other file-access functions know which file to work with.

Example:

<?php

$theFile = fopen("fileOne.txt", "a");

?>

In the above example, the variable $theFile is a handle for the file fileOne.txt.

Writing data to a text file

Writing data to a text file requires use of the fputs() function. This function takes two parameters - a file handle and a string of text.

Syntax:

fputs($fileHandle, $text);

The file handle refers to the file which is to be written to. The string of text refers to the data to be written to the file referenced by the file handle.

Example:

<?php

$theFile = fopen("fileOne.txt", "w");
fputs($theFile, "line of text");

?>

In the above example, a file name fileOne.txt is opened with write access. The handle for this file is a variable named $theFile. The fputs() function takes the file handle $theFile as its first parameter and the string "line of text" as its second parameter. The string "line of text" will be written to the file fileOne.txt and previous content will be erased.

Reading data from a text file

Reading data from a text file requires use of the fgets() function. This function takes one parameter - a file handle referring to the file to be read.

Syntax:

fgets($fileHandle)

NOTE: When working with the fgets() function, files should be set with the 'r' (read-only) access modifier.

Example:

<?php

$theFile = fopen("fileOne.txt", "r");
$theText = fgets($theFile);
print $theText;

?>

In the above example, a file named fileOne.txt is opened with read access. The variable $theText gets the data from fileOne.txt through the fgets() function which takes as its parameter the variable $theFile which is a file handle for fileOne.txt. The output of this example would be the text from the file fileOne.txt

Checking for the end of a file

When reading data from a text file, your script does not know the length of the file. The fgets() function reads one line of text at a time. To check for the end of a file, use the feof() function (which stands for file end of file). The feof() functions returns true if the end of the file has been reached, and false if the end of the file has not been reached.

Syntax:

feof($fileHandle);

The file handle refers to the file which to check if the end of the file has been reached.

Example:

<?php

//open a file named fileOne.txt with read access
//a file handle named $theFile will store this file
$theFile = fopen("fileOne.txt", "r");

/*

this while loop states that while
the end of the file has not been reached
the variable $lineOfText gets the text from
the file

the print command is then used to print each line of
text from the file followed by a line break specified
by HTML's <br /> tag

*/

while(!feof($theFile)){

$lineOfText = fgets($theFile);
print "$lineOfText<br />";

}

?>

The above example takes a file with some text, and uses the feof() function in conjunction with a while loop to print all the lines of text in the file until the end of the file has been reached.

NOTE: The ! symbol is an operator meaning "not". The feof() function returns true if an end of a file has been reached, so the condition for the while loop in the above example is if the end of the file has not been reached.

Closing a file

Closing a file requires use of the fclose() function.

Syntax:

fclose($fileHandle);

Example:

<?php

$theFile = fopen("fileOne.txt", "w");
fclose($theFile);

?>

In the above example, a file named fileOne.txt is opened with write access. The fclose() function takes as its parameter the $theFile variable which is a file handle for fileOne.txt and the file is closed.

NOTE: If a script ends without a file being explicitly closed, PHP may or may not automatically close the file for you. For this reason, it is important to use the fclose() function when working with files every time!

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments