Using the fopen()
function to open files is generally more advantageous than the readfile()
function because it offers greater flexibility and more options.
We will be using the text file named “webdictionary.txt” throughout the lessons.
AJAX = Asynchronous JavaScript and XML CSS = Cascading Style Sheets HTML = Hyper Text Markup Language PHP = PHP Hypertext Preprocessor SQL = Structured Query Language SVG = Scalable Vector Graphics XML = EXtensible Markup Language |
The first parameter of fopen()
specifies the name of the file to be opened, while the second parameter indicates the mode in which the file should be accessed. The example below also includes a message to be displayed if fopen()
fails to open the specified file.
<?php $myfile = fopen(“webdictionary.txt”, “r”) or die(“Unable to open file!”); echo fread($myfile,filesize(“webdictionary.txt”)); fclose($myfile); ?> |
Tip: The fread()
and fclose()
functions will be discussed later.
The file can be opened in one of the following modes:
Modes |
Description |
r |
Open a file for reading only. The file pointer begins at the start of the file. |
w |
Open a file for writing only. This will erase the file’s contents or create a new file if it does not exist. The file pointer starts at the beginning of the file. |
a |
Open a file for writing only. The existing data in the file is preserved, with the file pointer starting at the end of the file. A new file is created if it does not exist. |
x |
Creates a new file for writing only. Returns |
r+ |
Open a file for both reading and writing. The file pointer begins at the start of the file. |
w+ |
Open a file for both reading and writing. This will erase the file’s contents or create a new file if it does not exist. The file pointer starts at the beginning of the file. |
a+ |
Open a file for both reading and writing. The existing data is preserved, with the file pointer starting at the end of the file. A new file is created if it does not exist. |
x+ |
Creates a new file for both reading and writing. Returns |
The fread()
function is used to read from an open file.
The first parameter of fread()
is the file handle from which to read, and the second parameter specifies the maximum number of bytes to read.
The following PHP code reads the entire contents of the “webdictionary.txt” file:
fread($myfile,filesize(“webdictionary.txt”)); |
The fclose()
function is used to close a file that is currently open.
It is considered good programming practice to close all files once you are done using them. Keeping files open unnecessarily can consume server resources. |
The fclose()
function requires the file handle (or a variable containing the file handle) for the file you want to close.
<?php $myfile = fopen(“webdictionary.txt”, “r”); // some code to be executed…. fclose($myfile); ?> |
The fgets()
function reads a single line from a file.
The example below displays the first line of the “webdictionary.txt” file:
<?php $myfile = fopen(“webdictionary.txt”, “r”) or die(“Unable to open file!”); echo fgets($myfile); fclose($myfile); ?> |
Note: After calling the fgets()
function, the file pointer advances to the next line.
The feof()
function checks whether the “end-of-file” (EOF) has been reached.
It is useful for iterating through data of unknown length.
The example below reads the “webdictionary.txt” file line by line until the end-of-file is reached:
<?php $myfile = fopen(“webdictionary.txt”, “r”) or die(“Unable to open file!”); // Output one line until end-of-file while(!feof($myfile)) { echo fgets($myfile) . “<br>”; } fclose($myfile); ?> |
The fgetc()
function reads a single character from a file.
The example below reads the “webdictionary.txt” file one character at a time until the end-of-file is reached:
<?php $myfile = fopen(“webdictionary.txt”, “r”) or die(“Unable to open file!”); // Output one character until end-of-file while(!feof($myfile)) { echo fgetc($myfile); } fclose($myfile); ?> |
Note: After calling the fgetc()
function, the file pointer advances to the next character.