HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
In this chapter we will teach you how to open, read, and close a file on the server
A better method to open files is with the fopen()
function. This function gives you more
options than the readfile()
function.
We will use the text file, "webdictionary.txt", during the lessons:
The first parameter of fopen()
contains the name of the file to be opened and the
second parameter specifies in which mode the file should be opened. The following example
also generates a message if the fopen() function is unable 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);
?>
fread()
and the fclose()
functions will be
explained below.
The file may be opened in one of the following modes:
Modes | Description |
---|---|
r | Open a file for read only. File pointer starts at the beginning of the file |
w | Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a | Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x | Creates a new file for write only. Returns FALSE and an error if file already exists |
r+ | Open a file for read/write. File pointer starts at the beginning of the file |
w+ | Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a+ | Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x+ | Creates a new file for read/write. Returns FALSE and an error if file already exists |
The fread()
function reads from an open file.
The first parameter of fread()
contains the name of the file to read from and
the second parameter specifies the maximum number of bytes to read.
The following PHP code reads the "webdictionary.txt" file to the end:
The fclose()
function is used to close an open file.
It's a good programming practice to close all files after you have finished with them. You don't want an open file running around on your server taking up resources!
The fclose()
requires the name of the file (or a variable that holds the
filename) we want to close:
The fgets()
function is used to read a single line from a file.
The example below outputs 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);
?>
fgets()
function, the file pointer has moved to the next line.
The feof()
function checks if the "end-of-file" (EOF) has been reached.
The feof()
function is useful for looping through data of unknown length.
The example below reads the "webdictionary.txt" file line by line, until 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 is used to read a single character from a file.
The example below reads the "webdictionary.txt" file character by character, until 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);
?>
fgetc()
function, the file pointer moves to the next character.
For a complete reference of filesystem functions, go to our complete PHP Filesystem Reference.
Open a file, and write the correct syntax to output one character at the time, until end-of-file.
$myfile = fopen("webdict.txt", "r"); while(!($myfile)) { echo ($myfile); }