PHP includes functions that allow files to be read from, written to and deleted.
The fopen() Function
Before any operations can be carried out on a file it must be opened. The fopen() function takes two arguments.
The name of the file to be opened.
The mode, which specifies the type of access required. The mode may be any of those shown in the table below.
Mode
Description
'r'
Open for read only and place the file pointer at the beginning of the file.
'r+'
Open for read and write and place the file pointer at the beginning of the file.
'w'
Open for write only, place the file pointer at the beginning of the file and truncate the file length to zero. If the file doesn't exist then attempt to create it.
'w+'
Open for read and write, place the file pointer at the beginning of the file and truncate the file length to zero. If the file doesn't exist then attempt to create it.
'a'
Open for write only and place the file pointer at the end of the file. If the file doesn't exist then attempt to create it.
'a+'
Open for read and write and place the file pointer at the end of the file. If the file doesn't exist then attempt to create it.
'x'
Create and open for write only and place the file pointer at the beginning of the file. If the file already exists then fopen() fails and returns FALSE. If the file doesn't exist then attempt to create it.
'x+'
Create and open for read and write and place the file pointer at the beginning of the file. If the file already exists then fopen() fails and returns FALSE. If the file doesn't exist then attempt to create it.
Note: Opening a file for writing will fail if the appropriate file permissions have not been set for the file or the directory in which it's situated.
The fopen() function returns either ...
FALSE if an error occurs.
A handle that points to the file - required for future operations on the file.
Example of Opening a File for Reading
<?php
$handle = fopen ('my_file.txt', 'r') or exit ('Unable to open file');
?>
Closing a File
The fclose() function closes a file and only requires one argument - the file handle that was returned when the file was opened.
If a file is not closed then PHP will close it anyway when the script terminates, however, it's good programming practice to do so.
Example of Closing a File
<?php
fclose ($handle);
?>
Reading a File
The fread() reads the contents of a file and takes two arguments.
The file handle returned from fopen().
The number of bytes to read (1 byte = 1 character).
If you want to read the whole file but don't know its size then the filesize() function can be used, which takes the file name as an argument.
Examples of Reading a File's Contents into a Variable
<?php
$handle = fopen ('my_file.txt', 'r') or exit ('Unable to open file');
// Read the first 10 characters of the file
$contents = fread ($handle, 10);
fclose ($handle);
$handle = fopen ('my_file.txt', 'r') or exit ('Unable to open file');
// Read the whole file
$contents = fread ($handle, filesize ('my_file.txt');
fclose ($handle);
?>
Writing to a File
The fwrite() function writes to a file and takes two arguments with an optional third argument.
The file handle returned from fopen().
The string to write to the file.
The number of characters of the string to write.
If the 'w' mode is used when the file is opened then the file is overwritten. If the file doesn't already exist then it will be created.
If you wish to append the string to the file instead of overwriting the file then use the 'a' mode when opening the file.
Example of Writing to a File
<?php
$handle = fopen ('my_file.txt', 'a') or exit ('Unable to open file');
$append = ' the end.';
fwrite ($handle, $append);
fclose ($handle);
?>
Deleting a File
The unlink() function deletes a file and takes the path to the file as an argument.