Category Archives: PHP-File Handling - Page 2

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.