We can do things for files like reading, writing or appending. Before we start with files, we must open it first for reading, writing or both. PHP provides the fopen() function for doing so. fopen() requires a string that contains the file path and the mode like read (r), write (w) and append (a). Let’s see the example:

<?
$file='D:\xampp\htdocs\exprogram\filetxt\readme.txt';

	if (file_exists($file)){
		$of = fopen ($file, 'r');
		$read = fread ($of, filesize($file));
		echo $read.', panjang= ' .strlen($read);
		fclose($of);
	}
	else{
		echo 'The file is not exist';
	}
//eof

Function file_exists() is used for the existence of a file. If the file is found file_exists() return true, otherwise it returns false. Function fread() is for reading arbitrary amounts of data from a file through accept a file resource as an argument, as well as the number of bytes you want to read. Try to change line 6 above with $read = fread ($of, 10); then see the result. Assuming that all is well and you go on to work with your open file, you should remember to close it when you finish using fclose().

Checking the status of a file:
After checking that a file exists, you can find out some things that you can do with it. PHP can help you typically whether you can read, write to or execute this file.
is_readable() tells you whether you can read a file.
is_writable() tells you whether you can write to a file.
is_executable() tells you whether you can run a file, relying on either the file’s permissions or its extension depending on your platform.
All above function requires the file path and returns a Boolean value.

Getting date information about a file:
This function is used to find out when a file was last written to or accessed.
fileatime() to find out when a file was last accessed.
filemtime() to discover the modification date of a file.
filectime() to test the change time of a document.
Lets see the example:

<?
$file='D:\xampp\htdocs\exprogram\filetxt\readme.txt';
	$ftime = fileatime($file);
	echo $file. ‘ was last accessed on ";
	echo date(‘D d M Y g:i A’, $ftime);

Reading lines from a file:
To read a line from an open file you can use fgets(), which requires the file resource returned from fopen() as an argument like this:

<?
$file='D:\xampp\htdocs\exprogram\filetxt\readme.txt';
$of = fopen ($file, 'r');
	$line = fgets ($of);
	echo $line;
	fclose($of);

Although you can read lines with fgets(), you need some way to tell when you reach the end of the file. The feof() function does this by returning true when the end of the file has been reached and false otherwise. So you have enough information to read a file line by line as shown below:

<?
$file='D:\xampp\htdocs\exprogram\filetxt\readme.txt';
$of = fopen ($file, 'r') or die (‘Could not open file’);
	while (!feof($of)){
		$line = fgets($of, 10);
		echo $line;
}

Moving around a file:
Function fseek() let you decide the position manually from which the acquisition begins. fseek() enable you to change your current position within a file. It requires a file resource and an integer that represents the offset from the start of the file (in bytes) to which you want to jump. Lets see the example below:

<?
$file='D:\xampp\htdocs\exprogram\filetxt\readme.txt';
$of = fopen ($file, 'r') or die (‘Could not open file’);
	$fsize = filesize($file);
	$fhalf = (int)($fsize/2);
	Echo ‘Setengah file’. $fhalf;

	fseek($of, $fhalf);
	$ branch = fread($of, ($fsize - $fhalf));
	echo $branch;

Reading characters from a file:
With use fgetc() it return only a single character from a file every time it is called. Because a character is always one byte in size, fgetc() doesn’t require a length argument.

<?
$file='D:\xampp\htdocs\exprogram\filetxt\readme.txt';
$of = fopen ($file, 'r') or die (‘Could not open file’);
	while (!eof($of)){
		$char = fgetc($of);
		echo $char.’<br>’;
}

Reading entire file:
We can also read a file at one time use file_get_contents(). Lets take a look:

<?
$file='D:\xampp\htdocs\exprogram\filetxt\readme.txt';
$of = fopen ($file, 'r') or die (‘Could not open file’);
	$files = file_get_contents($file);
	echo $files;

That code will read entire file $file and saved into $files. But be careful if the size of file is very big :d.

Writing or Appending to a file:
Writing occurs from the start of the file, any prior content is destroyed and replaced by the data you write. You use mode argument ‘w’ when you call fopen().

<?
$file='D:\xampp\htdocs\exprogram\filetxt\readme.txt';
$of= fopen($file, ‘w’) or die (‘Could not open file’);
fwrite($of,’Hello World \n’);
fclose($of);

Appending are added the writes to the existing file. But if you have nonexistent file, the file will be created first.

<?
$file='D:\xampp\htdocs\exprogram\filetxt\readme.txt';
$of= fopen($file,’a’) or die (‘Could not open file’);
fputs($of, ’and another thing \n’);
fclose($of);

Locking files:
To locking files we can use flock() which locks a file to warn other processes against writing to or reading file while the current process is working with file. I mean you don’t need your file being corrupt because other user accessing files at the time. You should call flock() directly after calling fopen() and call it again to release the lock before closing the file. If the lock is not released, you will not be able to read or write to that file.

<?
$file='D:\xampp\htdocs\exprogram\filetxt\readme.txt';
	$of = fopen($file, ‘a’) or die (‘Could not open file’);
	flock($of, LOCK_EX); // exclusive lock
		// read or write to the file
	flock($of, LOCK_UN); // release the lock
	fclose($of);

There is another constant with type shared that is LOCK_SH. This type allows other processes to read the file but prevent writing (used when reading a file).