Here, you are going o introduce with the function of opening a file.
How to open?
It is necessary to inform PHP why you are going to unlock a file? We have given some ways of opening a file:
• Read: ‘r’
It will allow you to only read a file.
• Write: ‘w’
Here, you are allowed to write. Moreover, you can start from the beginning when there is no record in the file. The data can be erased.
• Append: ‘a’
It will unlock a file for only writing purpose. The data will remain save. You are only allowed to write at the end.
In order to prevent you from any trouble, file pointer is used. If the file has restriction of read only then the pointer will be at the top of the page. If your requirement is to add the data then the location of the pointer will be at the conclusion.
What are the diverse functions of fopen?
There are three main aims of opening a file in different ways. It depends on your requirement. If you need to get information or to read something then the file will get opened in read only mode.
The other option offers you to make some addition or modification in the file. Now, you have to make the file open with the option ‘w’. You will be given access to write in the file or erase the data from the file.
On the other hand, if you have to only add something at the end then you have to choose option ‘a’. Now, you can add any news or list of orders in the file.
Additional way of opening a File:
There are many other ways of opening a file. It is better to choose a way in which you can read and also write in the file. For this purpose, you have to use “+” sign.
• Read/Inscribe: ‘r+’
It will unlock the file for both reading and writing purpose.
• Inscribe /Read: ‘w+’
Here, you can write but all the previous data will be erased.
• Append: ‘a+’
Here the function is same but you can only write at the end.
An example of the opening a file
Here is an authentic code for file opening. The character ‘x’ can be changed with ‘a’, ‘r’ or ‘w’.
Code of PHP
$fh = fopen($newFileName, 'X') or die("file can not open");
Synopsis of file open:
You have given a choice to unlock a file in numerous ways. It is up to you to erase everything and start again or you can just write at the last part of previous data. You have also choice to unlock a file only for reading purpose.