file handling using php

Opening a file

fopen() :- this function used to open a file or url

Syntax :- fopen(filename,mode,include_path,context);

example

file.txt

Well Come To PHP World

file.php

<?php
	$handle=fopen("file.txt", "r");
	if ($handle) {
		echo "file is open";
	}
	else{
		echo "file is not open";
	}
?>

open a file

Reading a file

feof() :- This function test for end of file on a file handle.

Syntax :- feof($file_handle);

fgets() :- This function use to read a single line from a file

Syntax :- fgets($file_handle,length);

example

file1.php

<?php
	$handle=fopen("file.txt", "r");
	while (!feof($handle))
	{
		echo fgets($handle);
	}
	
?>

reading a file

Closing a file

fclose() function is used to close open file closing the files free up the resources connected with the file

Syntax : fclose($file_handle);

example

file2.php

<?php

	$handle=fopen("file.txt", "r");#open file
	
	while (!feof($handle))
	{
		echo fgets($handle);#read text from file
	}
	fclose($handle); #closing file 
?>

closing file