codescracker


php

PHP File Handling (I/O)



« Previous Tutorial Next Tutorial »


Here you will learn about file handling (input/output) in PHP with examples.

PHP file handling (i/o) becomes essential to learn when you are going to deal with files on your web or website using PHP program.

Using PHP, you can easily handle file on the web or on your website.

Types of File Handling Actions in PHP

Here are the list of action types used in file handling using PHP:

You will learn all about the above file handling tutorials divided into separate tutorials. But for now, let's take some examples on file handling in PHP.

PHP File Handling Example

Create a file named codescracker.txt and place the following content inside it.

You are learning file handling tutorial
with examples on codescracker.com
I am a file.
I will be opened and displayed on the 
web using my favourite web programming 
language, that is, PHP.
Thank You!

Here is the screenshot of the file with content typed in Notepad text editor:

php file handling

Save the codescracker.txt file in C:\xampp\htdocs\ directory.

Now create a file named codescracker.php and place the following content inside it.

<html>
<head>
	<title>PHP file handling example - CodesCracker</title>
</head>
<body>
<?php
	// This is just demo example on 
	// how to handle file on web using PHP
	// Below code will open file using 
	// fopen() function of PHP, here r is given
	// to open the file for reading purpose only
	$my_file = fopen("codescracker.txt", "r");
	// check if file exist/open or not
	if(!empty($my_file))
	{
		// read all the content of that file
		while(!feof($my_file))
		{
			// print the content of file
			echo fgets($my_file). "<br/>";
		}
	}
	// print error message if file doesn't exists/open
	else
	{
		echo "Error occurred in opening the file<br/>";
		echo "Make sure that the file exists in the current directory</br/>";
		echo "or try again to open and read file";
	}
	// close file
	fclose($my_file);
?>
</body>
</html>

Now save the file named codescracker.php inside the same directory as provided above.

Here is the screenshot of the directory folder containing both codescracker.php (main file contains file handling code in PHP) and codescracker.txt (this is the file to handle).

php file directory

Now open any browser, and type localhost/codescracker.php to check the output produced by the above file handling example in PHP. Here is the sample output produced by the above file handling program using PHP.

php file handling example

If you open any non-existing file using the above PHP file handling program, then the content of the else block will be executed. Let's modify the above file handling program and try to open non-existing file as shown below.

<html>
<head>
	<title>PHP file handling example - CodesCracker</title>
</head>
<body>
<?php
	$my_file = fopen("unknown.txt", "r");
	if(!empty($my_file))
	{
		while(!feof($my_file))
		{
			echo fgets($my_file). "<br/>";
		}
	}
	else
	{
		echo "Error occurred in opening the file<br/>";
		echo "Make sure that the file exists in the current directory</br/>";
		echo "or try again to open and read file";
	}
	fclose($my_file);
?>
</body>
</html>

As the file unknown.txt doesn't exist inside the current directory (directory where codescracker.php exist), therefore you will see an error message like this:

file handling in php

If you don't want to see irritating error message, then add the following one line of code at beginning.

error_reporting(0);

Here is the modified and short version of the above file handling example code in PHP.

<html>
<body>
<?php
	error_reporting(0);
	$my_file = fopen("unknown.txt", "r");
	if(!empty($my_file))
	{
		while(!feof($my_file))
		{
			echo fgets($my_file). "<br/>";
		}
	}
	else
	{
		echo "Error occurred in opening the file<br/>";
		echo "Make sure that the file exists in the current directory</br/>";
		echo "or try again to open and read file";
	}
	fclose($my_file);
?>
</body>
</html>

This time, you will see the following output:

file handling program php

PHP Complete File Handling Program

From next tutorial, you will learn all about file handling step by step from creating file to deleting file using PHP.

But for now, let's take a complete file handling program in PHP. Complete file handling program means a file handling program that includes following tasks:

You will learn about all the above topics/tasks of file handling divided into separate tutorials.

Now, here is a complete file handling program in PHP.

<html>
<head>
	<title>Complete File Handling Program in PHP</title>
</head>
<body>
<?php
	// PHP complete file handling program
	$filename = "codescracker.txt";
	// open and write to file
	$filehandler = fopen($filename, "w");
	if($filehandler == false)
	{
		echo "error occurred...exiting...";
		exit();
	}
	fwrite($filehandler, "<p>This is complete file handling program.</p>");
	// close file
	fclose($filehandler);
	// open and read from file
	$filehandler = fopen($filename, "r");
	if($filehandler == false)
	{
		echo "error occurred...exiting...";
		exit();
	}
	while(!feof($filehandler))
	{
		echo fgets($filehandler);
	}
	// close file
	fclose($filehandler);
	// delete file
	unlink($filename);
?>
</body>
</html>

Here is the sample output produced by the above complete file handling program using PHP.

complete file handling program php

You can't see the created file when running the above program as the file is created and deleted within fraction of milli seconds. Therefore to see the file inside the current directory before deleting the file using the above program, just place the following code before deleting the file or before unlink() function used in the above complete file handling program.

for($i=0;$i<900000000;$i++){}

Above code gives you some time to see the created file inside the current directory before the file is deleted.


« Previous Tutorial Next Tutorial »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test