These three basic ways to open a file have distinct purposes. If you want to get information out of a file, like search an e-book for the occurrences of “cheese”, then you would open the file for read only. If you wanted to write a new file, or overwrite an existing file, then you would want to open the file with the “w” option. This would wipe clean all existing data within the file. If you wanted to add the latest order to your “orders.txt” file, then you would want to open it to append the data on to the end. This would be the “a” option.
Open a file for read only use. The file pointer begins at the front of the file.
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.
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.
There are additional ways to open a file. You can open a file in such a way that reading and writing is allowable! This combination is done by placing a plus sign “+” after the file mode character.
Opens a file so that it can be read from and written to. The file pointer is at the beginning of the file.
This is exactly the same as r+, except that it deletes all information in the file when the file is opened.
This is exactly the same as r+, except that the file pointer is at the end of the file.
PHP Code – File Open
<?php $FileName = "testFile.txt"; $fv = fopen($FileName, 'r') or die("Can't open file"); fclose($fv); ?>