Advertisement
Google Ad Slot: content-top
PHP File Open/Read/Close
In PHP, files can be opened, read, and closed using various functions like fopen(), fread(), and fclose(). These are essential for performing file operations safely and efficiently.
We will use the text file, "sample.txt", during the lessons:
PHP Open File - fopen()
The fopen() function is used to open a file. It requires two parameters:
- Filename → The file path.
- Mode → Specifies how the file should be opened (read, write, append, etc.).
The file may be opened in one of the following modes:
Modes |
Description |
|---|---|
r |
Read-only. Starts at the beginning of the file. File must exist. |
r+ |
Read and write. Starts at the beginning of the file. File must exist. |
w |
Write-only. Opens and clears the contents. Creates a new file if it doesn't exist. |
w+ |
Read and write. Clears the contents of the file. Creates a new file if it doesn't exist. |
a |
Write-only. Opens the file and writes at the end (append). Creates the file if it doesn't exist. |
a+ |
Read and write. Opens the file in append mode. Creates the file if it doesn't exist. |
x |
Write-only. Creates a new file. Returns |
x+ |
Read and write. Creates a new file. Returns |
c |
Write-only. Opens the file if it exists or creates a new one. Does not delete existing data. |
c+ |
Read and write. Opens or creates a file without deleting its content. |
PHP Read File - fread()
After opening a file, use fread() to read its contents.
fread() requires two parameters:
- The file pointer (returned by
fopen()). - The number of bytes to read, typically using
filesize().
The following PHP code reads the "example.txt" file to the end:
PHP Close File - fclose()
It is important to close the file using fclose() after reading or writing to free up resources.
The fclose() requires the name of the file (or a variable that holds the filename) we want to close:
PHP Read Single Line - fgets()
The fgets() function in PHP is used to read a single line from an open file.
Syntax:
fgets(file, length)
Parameters:
file(Required) → A valid file pointer, typically returned byfopen().length(Optional) → The number of bytes to read. If not specified, it will read until it reaches a newline (\n) or the end of the file (EOF).
he example below outputs the first line of the "example.txt" file:
Note
After a call to the fgets() function, the file pointer has moved to the next line.
PHP Check End-Of-File - feof()
The feof() function in PHP is used to check whether the end of a file (EOF) has been reached.
Syntax:
feof(file)
Parameters:
file(Required) → A valid file pointer returned byfopen()orpopen().
Return Value:
- Returns
trueif the end of the file (EOF) has been reached. - Returns
falseif it hasn't reached the end of the file.
PHP Read Single Character - fgetc()
The fgetc() function in PHP is used to read a single character from an open file. It is useful when you need to read a file character by character.
Syntax:
fgetc(file)
Parameters:
file(Required) → A valid file pointer returned byfopen().
Return Value:
- Returns the next character from the file on success.
- Returns
falseon end of file (EOF) or error.