Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

PHP

Learn step by step



PHP file input/output


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");

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

  • r - Denotes that the file is read-only, the data within it can only be read and it can not be written to.
  • w - Denotes that the file can be written to. With this file access modifier any data you write to the file will erase the previous data that was in the file before.
  • a - Denotes that the file can be written to. With this file access modifier any data you write to the file will be appended to it as opposed to erasing the previous data that was in the file before.

<?php
fopen("fileOne.txt", "a");
?>
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.

<?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.

<?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.

<?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.

<?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 />";

}

?>
Closing a file
<?php
$theFile = fopen("fileOne.txt", "w");
fclose($theFile);
?>


Related Videos