Tuesday, April 22, 2025

PHP Reading Files

You are advised to check corresponding YouTube video at the end of this article.

There are several file handling functions in PHP, but some of the most commonly used ones are:

  1. fopen(): Used to open a file and returns a file pointer resource that is used in other file handling functions.

  2. fclose(): Used to close an opened file.

  3. fwrite(): Used to write data to an opened file.

  4. fread(): Used to read data from an opened file.

  5. file_get_contents(): Used to read the entire contents of a file into a string.

  6. file_put_contents(): Used to write a string to a file.

  7. file_exists(): Used to check if a file exists.

  8. unlink(): Used to delete a file.

These functions allow developers to create, read, update, and delete files in PHP.

source.txt


first line
second line
third line

index.php or open_file.php, you decide.


<?php

$result = fopen("source.txt" , "r");

echo fread($result, filesize("source.txt"));

fclose($result);

?>

This code opens the file "source.txt" in read mode using the fopen() function, reads the content of the file using the fread() function, and then closes the file using the fclose() function.

The fread() function reads the content of the file up to the specified number of bytes, which is determined by the filesize() function that returns the size of the file in bytes.

The content of the file is then output to the screen using the echo statement.

This code can be used to display the content of the file "source.txt" on a web page. 


<?php

$result = fopen("source.txt" , "r");

echo fgets($result) . "<br>";
echo fgets($result) . "<br>";
echo fgets($result) . "<br>";

fclose($result);

?>

This code reads and outputs the first three lines of the "source.txt" file using the fgets() function. Here's what's happening:

  • fopen("source.txt" , "r"): This opens the "source.txt" file in read mode and returns a file pointer to the beginning of the file. This file pointer is stored in the $result variable.
  • fclose($result): This closes the file and frees up the file pointer.

So, assuming the "source.txt" file has at least three lines of text, this code will output those three lines to the screen. 


<?php

$result = fopen("source.txt" , "r");

for ($x = 0; $x < 5; $x = $x + 1) {
	echo fgets($result)	. "<br>";
}

fclose($result);

?>

This code opens the file "source.txt" for reading and then reads and outputs the first 5 lines of the file using a for loop and the fgets function.

The fgets function reads a line from the file pointed to by the file handle ($result in this case) and returns it as a string. Each time the function is called, it reads the next line from the file.

In this code, the fgets function is called 5 times inside a for loop that runs 5 times. Each time the loop runs, it calls fgets to read the next line and then outputs that line with an HTML line break (<br>). 


<?php

$result = fopen("source.txt" , "r");

$x = 0;

while ($x < 4) {
	echo fgets($result) . "<br>";
	$x = $x + 1;
}

fclose($result);

?>

Note that the loop condition checks if $x is less than 4 because the loop should only run for 4 iterations to print the first 4 lines. 


<?php

$result = fopen("source.txt" , "r");

while (!feof($result)) {
	echo fgets($result) . "<br>";
}

fclose($result);

?>

The feof() function in PHP checks if the end of the file has been reached while reading a file using a file pointer. It stands for "end of file".

The feof() function takes one argument, which is the file pointer returned by fopen() when opening a file. It returns true if the file pointer has reached the end of the file, and false otherwise.

A common use of feof() is to read a file line by line using a loop and fgets(), and terminate the loop when feof() returns true, indicating the end of the file has been reached. 


<?php

$result = fopen("source.txt" , "r") or 
die ("Unable to find that file");

while (!feof($result)) {
	echo fgets($result) . "<br>";
}

fclose($result);

?>

If the file does not exist or cannot be opened for any reason, the code after the 'or' operator will be executed, which in this case is the die() function with an error message "Unable to find that file". The die() function will terminate the execution of the script immediately and display the error message.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...