codescracker


php

PHP Read File



« Previous Tutorial Next Tutorial »


To read content from a file in PHP, use fread() function. You can also use fgets() and fgetc() to read a file line by line and character by character.

Ways to Read a File in PHP

Here are the list of three ways, you can go for, to read a file in PHP:

PHP Read File Example

Here is an example of reading a file directly using fread() function in PHP.

Before going to example of reading a file, let's first create a file named codescracker.txt and place the following content inside it.

<p>This is reading a file tutorial in PHP.</p> <p>Here you will learn about how to read a file in PHP both line by line and character by character.</p> <p>Every topics in this reading file tutorial contains its respective examples in PHP.</p>

Save this file inside the current directory folder, that is, C:\xampp\htdocs\. Here is the screenshot of the content of file codescracker.txt

php read file

Now, below is an example used to read the file using PHP.

<html>
<head>
	<title>Reading a file using PHP Example</title>
</head>
<body>
<?php
	// initialize filename to a php variable 
	$myreadfile = "codescracker.txt";
	// opens the file in reading mode
	$myfilehandle = fopen($myreadfile, "r");
	// checks if file is opened or not
	if($myfilehandle == false)
	{
		// putting error message
		echo "Error occurred...exiting...";
		exit();
	}
	// get file size of codescracker.txt
	$size_of_file = filesize($myreadfile);
	// now read the content of the file
	$content_in_file = fread($myfilehandle, $size_of_file);
	echo $content_in_file;
	// now close the file 
	fclose($myfilehandle);
?>
</body>
</html>

Here is the sample output produced by the above reading a file example code in PHP.

reading a file in php

Reading a File Line by Line in PHP

To read a file line by line in PHP, use fgets() function. Here is an example shows how to read any file line by line in PHP.

<html>
<head>
	<title>Reading a file line by line using PHP Example</title>
</head>
<body>
<?php
	$myfilehandler = fopen("codescracker.txt", "r");
	if($myfilehandler == false)
	{
		echo "Error in opening the file...exiting...";
		exit();
	}
	while(!feof($myfilehandler))
	{
		echo fgets($myfilehandler);
	}
	fclose($myfilehandler);
?>
</body>
</html>

The output produced by the above reading a file line by line example code in PHP is given below:

read file line by line

Reading a File Character by Character in PHP

To read any file character by character in PHP, then use PHP fgetc() function. Here is an example demonstrates about reading file character by character.

<html>
<head>
	<title>Reading a file character by character in PHP</title>
</head>
<body>
<?php
	$filecychandle = fopen("codescracker.txt", "r");
	if($filecychandle == false)
	{
		echo "Error in opening the file...exiting...";
		exit();
	}
	while(!feof($filecychandle))
	{
		echo fgetc($filecychandle);
	}
	fclose($filecychandle);
?>
</body>
</html>

The output of the above reading a file character by character example code in PHP is given here:

reading file character by character

« Previous Tutorial Next Tutorial »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test