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:

Hello, welcome to PHP File Handling.
This is a simple example of using fopen, fread, and fclose.

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.).
<?php
// Open the file in read mode
$file = fopen("sample.txt", "r");
// Check if file opened successfully
if ($file) {
// Read and display the content
$content = fread($file, filesize("example.txt"));
echo "File Content:<br>" . nl2br($content);
// Close the file
fclose($file);
} else {
echo "Error: Unable to open the file.";
}
?>

Try it yourself

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 FALSE and shows an error if the file already exists.

x+

Read and write. Creates a new file. Returns FALSE if the file already exists.

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:

fread($file,filesize("example.txt"));

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
$myfile = fopen("sample.txt", "r");
// some code to be executed....
fclose($myfile);
?>

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:

  1. file (Required) → A valid file pointer, typically returned by fopen().
  2. 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:

<?php
$file = fopen("sample.txt", "r") or die("Unable to open file!");
echo fgets($file);
fclose($file);
?>

Try it yourself

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 by fopen() or popen().

Return Value:

  • Returns true if the end of the file (EOF) has been reached.
  • Returns false if it hasn't reached the end of the file.
<?php
$file = fopen("sample.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($file)) {
echo fgets($file) . "<br>";
}
fclose($file);
?>

Try it yourself

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 by fopen().

Return Value:

  • Returns the next character from the file on success.
  • Returns false on end of file (EOF) or error.
<?php
$file = fopen("sample.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($file)) {
echo fgetc($file);
}
fclose($file);
?>

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.