This chapter will explain how to do the following functions with files:
- Opening a file
- Reading a file
- Writing a file
- Closing a file
How to Open and Close Files
With PHP’s fopen() function, a file is opened. It needs two arguments, which are the name of the file and the mode in which to work.
One of the six options in this table can be used to set the mode of a file.
Let’s say that we have a text file on the server called “webhelpers.txt” that looks like this:
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
Here is the PHP code to read the file and write it to the output buffer (if the readfile() function works, it will return the number of bytes read):
Example
<!DOCTYPE html>
<html>
<body><?php
echo readfile(“webhelpers.txt”);
?></body>
</html>
Output
Modes | Description |
---|---|
r | Open a file for read only. Open a file that can only be read. The file pointer starts at the file’s beginning. |
w | Open a file for write only. Deletes the file’s contents or, if it doesn’t already exist, makes a new file. The file pointer starts at the file’s beginning. |
a | Open a file for write only. The existing data in file is preserved. The file pointer starts at the file’s beginning. If the file does not already exist, it makes a new one. |
x | Creates a new file for write only. If the file already exists, returns FALSE and an error. |
r+ | Open a file for read/write. The file pointer starts at the file’s beginning. |
w+ | Open a file for read/write. Deletes the file’s contents or, if it doesn’t already exist, makes a new file. The file pointer starts at the file’s beginning. |
a+ | Open a file for read/write. The data that is already in the file is kept. The end of the file is where the file pointer starts. If the file does not already exist, it makes a new one. |
x+ | Creates a new file for read/write. If the file already exists, returns FALSE and an error. |
PHP Reads File – fread ()
The open file is read from by the fread() function.
The first parameter of fread() is the name of the file to read from. The second parameter tells fread() how many bytes it can read at most.
The “webhelpers.txt” file is read all the way to the end by the following PHP code:
fread($myfile,filesize(“webhelpers.txt”));
PHP Close File – fclose ()
fclose() is used to close a file that is already open.
When programming, it’s best to close all files when you’re done with them. You don’t want an open file running around on your server and using up resources.
The fclose() function needs the name of the file we want to close or a variable that holds the name of the file:
Example
<?php
$myfile = fopen(“webhelpers.txt”, “r”);
// some code to be executed….
fclose($myfile);
?>
PHP Read Single Line – fgets ()
With the fgets() function, a single line from a file is read.
The first line of the “webdictionary.txt” file is shown in the example below:
Example
<!DOCTYPE html>
<html>
<body><?php
$myfile = fopen(“webhelpers.txt”, “r”) or die(“Unable to open file!”);
echo fgets($myfile);
fclose($myfile);
?></body>
</html>
Output
Note: The file pointer has moved to the next line after calling the fgets() function.
PHP End-Of-File Check – feof ()
The feof() function checks to see if the “end-of-file” (EOF) has been reached.
The feof() function can be used to loop through data whose length is unknown.
The “webdictionary.txt” file is read line by line in the “Example” below, until the end-of-file is reached:
Example
<!DOCTYPE html>
<html>
<body><?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);
?></body>
</html>
Output
PHP Reads One Character at a Time – fgetc ()
With the fgetc() function, you can read one character from a file.
The following example reads the “webdictionary.txt” file one character at a time until it reaches the end of the file:
Example
<!DOCTYPE html>
<html>
<body><?php
$myfile = fopen(“webhelpers.txt”, “r”) or die(“Unable to open file!”);
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?></body>
</html>
Output
Note: When the fgetc() function is called, the file pointer moves to the next character in the file.