PHP Files & I/O operation

File Opening Modes

To open a file PHP you can use the fopen() function. This function takes two parameters, where the first parameter contains the name of the file and the second parameter is the modes that should be used to open the file.

Files modes can be specified as one of the six options in this table.

Mode Purpose
r Opens the file for reading only.
Places the file pointer at the beginning of the file.
r+ Opens the file for reading and writing.
Places the file pointer at the beginning of the file.
w Opens the file for writing only.
Places the file pointer at the beginning of the file.
and truncates the file to zero length. If files does not
exist then it attemts to create a file.
w+ Opens the file for reading and writing only.
Places the file pointer at the beginning of the file.
and truncates the file to zero length. If files does not
exist then it attemts to create a file.
a Opens the file for writing only.
Places the file pointer at the end of the file.
If files does not exist then it attemts to create a file.
a+ Opens the file for reading and writing only.
Places the file pointer at the end of the file.
If files does not exist then it attemts to create a file.

Opening a File

If the fopen() function is unable to open the specified file, it returns 0 (of false).

<?php
$file=fopen(“text.txt”,”w”);
? >

Closing a File

<?php
$file = fopen(“text.txt”,”r”);

//File operation on text file

fclose($file);
?>

Reading a File by line:

The fgets() function is used to read a single line from a file. After the line has been read the file pointer is pointing to the next line in the file. This is very useful, because we could now read the file line by line.

Before we show you the example we have to talk about the feof() function. This function can be used to check if the “End-Of-File” (EOF) has been reached. This is very useful because we now can loop through a file of unknown length. We can do this on every file that is not opened in w, a, and x mode!

<?php
$file = fopen(“test.txt”, “r”) or exit(“Unable to open the file!”);
while(!feof($file))
{
echo fgets($file). “<br />”;
}
fclose($file);
?>

Reading a File by Character

The function fgetc() can be used to read a character from a file. We now can use the previous example to read a file character by character. Take a look at the following example:

<?php
$file = fopen(“text.txt”, “r”) or exit(“Unable to open the file!”);
while(!feof($file))
{
echo fgetc($file);
}
fclose($file);
?>

As you can see it is almost the same as the previous example. The only thing that’s changed is the function fgets() in fgetc() and the html break is removed.

Writing a file

A new file can be written or text can be appended to an existing file using the PHP fwrite() function. This function requires two arguments specifying a file pointer and the string of data that is to be written.

<?php
$filename = “sample.txt”;
$file = fopen( $filename, “w” );
if( $file == false )
{
echo ( “Error in opening new file” );
exit();
}
fwrite( $file, “This is  a simple test\n” );
fclose( $file );
?>

Delete a File

$me_file = ‘sample.txt’;
unlink($me_file);

Leave a Reply