Thursday, March 12, 2009

Opening and closing files for read/write operations

PH{ has a set of functions which allows you to read / write in the file and after completing reading and writing you can close the file.

The following are the most important functions used for this type of operation:

fopen(): Opens a file
fgets(): Reads the contents of a file, normally one line at a time
fread(): Reads a specified amount of a file
fwrite(): Writes to a file
feof(): Determines whether the end of the file has been reached
rewind(): Moves an internal pointer back to the top of the file
fclose(): Closes a file.

Before creating a file is very easier, But we need to understand of how the file has been creating, There are different modes which allows file to be created.

Mode for reading and writing in a file

1. Type : readonly
Mode : I
Description: Internal pointer initially placed at beginning of file.

2. Type : Write-only
Mode : W
Description: Existing data deleted before writing. Creates a file if it
doesn’t already exist.
Mode : a
Description: Append mode. New data added at end of file. Creates a file
if it doesn’t already exist.
Mode : X
Description: Creates a file only if it doesn’t already exist, so no danger of
deleting existing data.

3. Type : Read/write
Mode : r+
Description: Read/write operations can take place in either order and
begin wherever the internal pointer is at the time. Pointer
initially placed at beginning of file. File must already exist for
operation to succeed.
Mode : w+
Description: Existing data deleted. Data can be read back after writing.
Creates a file if it doesn’t already exist.
Mode : a+
Description: Opens a file ready to add new data at end of file. Also
permits data to be read back after internal pointer has been
moved. Creates a file if it doesn’t already exist.
Mode : x+
Description: Creates a new file, but fails if a file of the same name already
exists. Data can be read back after writing.

Choose the wrong mode, and you could end up overwriting or deleting valuable data. You also need to be careful about the position of the internal pointer. If the pointer is at the end of the file, and you try to read the contents, you’ll end up with nothing. On the other hand, if the pointer is at the beginning of the file, and you start writing, you’ll overwrite the equivalent amount of any existing data.

You will need to pass two arguements when working with the FOPEN() method

1. The pathname to the file you want to open
2. Mode of the file.

Example:

$file = fopen('C:/test/test.txt', 'r');

Reading a file with FOPEN()

<?php
// store the pathname of the file
$filename = 'C:/test/test01.txt';
// open the file in read-only mode
$file = fopen($filename, 'r');
// read the file and store its contents
$contents = fread($file, filesize($filename));
// close the file
fclose($file);
// display the contents
echo nl2br($contents);
?>

Here in the above example we used fread() instead of file_get_contents(), One thing we need to know when we use the fread() is we need to know how much content we need to read, So for that case we will be supplying the second arguement as number of bytes which are to be read

The nl2br() function in the final line converts new line characters to XHTML
tags.

The other way to read the contents of a file with fopen() is to use the fgets() function, which retrieves one line at a time. This means that you need to use a while loop in combination with feof() to read right through to the end of the file. This is done by replacing this line

$contents = fread($file, filesize($filename));
with this (the full script is in fopen_readloop.php)
// create variable to store the contents
$contents = '';
// loop through each line until end of file
while (!feof($file)) {
// retrieve next line, and add to $contents
$contents .= fgets($file);
}

The while loop uses fgets() to retrieve the contents of the file one line at a time—
!feof($file) is the same as saying until the end of $file—and stores them in $contents.

Note: you need to use either fread() or fgets() if you want to read the contents of a file at the same time as writing to it.

0 comments:

Post a Comment