JavaScript Tutorial

File Handeling

File handling plays a very important role while storing & retrieving a wide variety of data. Several functions are there for creating, editing, reading and uploading files.

Opening a file

fopen() function is used to open files in PHP. By using this method, a file can be opened in various modes.

Mode

Description

r

Opens for read only, places the file pointer at the beginning of the file. 

w

Opens for writing only, places the file pointer at the beginning of the file and truncates the file to zero length. If the file does not exist, attempt to create it. 

a

Opens for writing only, places the file pointer at the end of the file. If the file does not exist, attempt to create it.

x

Creates a new file for writing only. Returns FALSE and an error if file already exists.

r+

Opens for read/write, places the file pointer at the beginning of the file.

w+

Opens for read/write. Erases the content of the file or creates a new file if it doesn't exist, places the file pointer at the beginning of the file.

a+

Opens for read/write. The existing data in the file is preserved and places the file pointer at the end of the file. Creates a new file if the file doesn't exist.

x+

Creates a new file for read/write. Returns FALSE and an error if file already exists

fopen() syntax

// syntax
fopen(filename, mode);

Code example:

<?php
	$file = "d:\\file.txt";
	$pointer = fopen($file, "r");
	fclose($pointer);
?>

Reading and Closing a file

There are various functions in PHP to read a file. These functions allow us to read the data line by line, character by character or all file data at once. These functions are:

fread(): This method reads the data from an open file. 

// syntax
fread(stream, length);

// UNDERSTANDING OF SYNTAX
// stream: A file system pointer resource that is created using fopen().
// length: length of byte to be read.

Code Example

<?php
	$file = "d:\\file.txt";
	$pointer = fopen($file, "r");
	$content = fread($pointer, filesize($file));
	fclose($pointer);		// closing file
?>

fgets(): This method reads a single line from the file.

// syntax
fgets(stream, length);		

// UNDERSTANDING SYNTAX
// stream: A file system pointer resource that is created using fopen().
// length: Reading ends when (length-1) bytes are read or a newline or an EOF occurs.

Code Example

<?php
	$file = "d:\\file.txt";
	$pointer = fopen($file, "r");   // opening file
	echo fgets($pointer);  // reading file
	fclose($pointer);	// closing file
?>

fgetc(): This method reads a single character from the file.

// syntax
fgetc(stream);		

// UNDERSTANDING SYNTAX
//stream: A file system pointer resource that is created using fopen().

Code Example

<?php
	$file = "d:\\file.txt";
	$pointer = fopen($file, "r");
	while( !feof($file)) {		//  Check End Of File (EOF)
		echo fgetc($file);
	}
	fclose($pointer);	// closing a file
?>

Writing a file

fwrite() method is used to write to a file.

// syntax
fwrite(stream, string, length);

Understanding syntax

stream: A file system pointer resource created using fopen().

string: A string to be written.

length: If this argument is given, writing will stop specified bytes are written or the end of string is reached. 

Code Example

<?php
	$file = fopen("file.txt", "w");
	fwrite($file, "Writing String to a file…\n");
	fwrite($file, "Adding another line…\n");
	fclose($file);
?>

Overwriting a file

file.txt is now composed of above mentioned data and if we will open this existing file in a writing mode, the existing data will get erased and a new file will be there to write again.

Code Example

<?php
	$file = fopen("file.txt", "w");
	$txt = "Mickey Mouse\n";
	fwrite($file, "New text…..\n");
	fclose($file);
?> 

Now, the file.txt file will contain “New text….” as its content. In a similar way, we can open a file in append mode to truncate the data into a file rather than erasing the older data. 

// for example
$file = fopen("file.txt", "a");

Deleting a file

unlink() function is used to delete a file.

// syntax
unlink(filename);

// ***It will return TRUE, if the file is deleted successfully.***

Code Example

<?php
	$res = unlink(‘file.txt’);
	if($res){
		echo “File deleted successfully”;
	}
?>
Go back to Previous Course