Monthly Archives: February 2011 - Page 2

PHP – File Read: fread Function

The fread function is the staple for getting data out of a file. The function requires a file handle, which we have, and an integer to tell the function how much data, in bytes, it is supposed to read.
One character is equal to one byte. If you wanted to read the first five characters then you would use five as the integer.
PHP Code:
$myFile = “filename.txt”;
$fh = fopen($myFile, ‘r’);
$theData = fread($fh, 5);
fclose($fh);
echo $theData;
Display:
kisor
The first five characters from the filename.txt file are now stored inside $theData. You could echo this string, $theData, or write it to another file.
If you wanted to read all the data from the file, then you need to get the size of the file. The filesize function returns the length of a file, in bytes, which is just what we need! The filesize function requires the name of the file that is to be sized up.
PHP Code:
$myFile = “filename.txt”;
$fh = fopen($myFile, ‘r’);
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;
Display:
Kisor rawan

PHP – File Write: Appending Data

Using the filename.txt file we created in the File Write lesson , we are going to append on some more data.
PHP Code:
$myFile = “filename.txt”;
$fh = fopen($myFile, ‘a’) or die(“can’t open file”);
$stringData = “laxmi\n”;
fwrite($fh, $stringData);
$stringData “khem\n”;
fwrite($fh, $stringData);
fclose($fh);
You should noticed that the way we write data to the file is exactly the same as in the Write lesson. The only thing that is different is that the file pointer is placed at the end of the file in append mode, so all data is added to the end of the file.
The contents of the file filename.txt would now look like this:
phpfresher
Kisor
laxmi
bikesh

PHP – File Write: fwrite Function

We can use php to write to a text file. The fwrite function allows data to be written to any type of file. Fwrite’s first parameter is the file handle and its second parameter is the string of data that is to be written. Just give the function those two bits of information and you’re good to go!
Below we are writing a couple of names into our test file filename and separating them with a carriaged return.
PHP Code:

$myFile = "filename.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "phpfresher";
fwrite($fh, $stringData);
fclose($fh);

The $fh variable contains the file handle for filename.txt. The file handle knows the current file pointer, which for writing, starts out at the beginning of the file.
We wrote to the file filename.txt twice. Each time we wrote to the file we sent the string $stringData that first contained mahohar khatri and second contained saroj karki. After we finished writing we closed the file using the fclose function.
If you were to open the filename.txt file in NOTEPAD it would look like this:
phpfresher

PHP – Different Ways to Open a File

For many different technical reasons, PHP requires you to specify your intentions when you open a file. Below are the three basic ways to open a file and the corresponding character that PHP uses.
• Read: ‘r’
Open a file for read only use. The file pointer begins at the front of the file.
• Write: ‘w’
Open a file for write only use. In addition, the data in the file is erased and you will begin writing data at the beginning of the file. This is also called truncating a file, which we will talk about more in a later lesson. The file pointer begins at the start of the file.
• Append: ‘a’
Open a file for write only use. However, the data in the file is preserved and you begin will writing data at the end of the file. The file pointer begins at the end of the file.
Summary

r Opens the file for reading only, and begin reading at the start of the file
r+ Opens the file for reading and writing, beginning at the start of the file
w Opens the file for writing only, and deletes everything in the file. If the file doesn’t exist, PHP will also try to create it.
w+ Opens the file for reading and writing and deletes everything within the file. If the file doesn’t exist, PHP will so try to create it.
a Open the file for writing only, beginning at the end of the file
a+ Open the file for reading and writing, beginning at the end of the file.

PHP-fopen() and fclose()

Fopen():
This function creates the file if the file doesn’t exists . The fopen function needs two important pieces of information to operate correctly.
1. we must supply it with the name of the file that we want it to open.
2. we must tell the function what we plan on doing with that file (i.e. read from the file, write information, etc).
Example Code:
$fileName = “filename.txt”;
$fileHandle = fopen($fileName, ‘w’) or die(“can’t open file”);
fclose(fileHandle);

• $fileName = “filename.txt”;
Here we create the name of our file, “filename.txt” and store it into a PHP String variable $fileName.
• $fileHandle = fopen($filnename, ‘w’) or die(“can’t open file”);
This bit of code actually has two parts. First we use the function fopen and give it two arguments: our file name and we inform PHP that we want to write by passing the character “w”.
Second, the fopen function returns what is called a file handle, which will allow us to manipulate the file. We save the file handle into the $fileHandle variable. We will talk more about file handles later on.
• fclose($fileHandle);
We close the file that was opened. fclose takes the file handle that is to be closed.