The fopen () function is used to open a file in PHP.
Open a file
The fopen () function is used to open a file in PHP.
The first parameter of this function contains the name of the file to be opened, and the second parameter specifies the mode used to open the file:
The file may be opened in the following mode:
| Mode |
Description |
| R |
Read-only. Start at the beginning of the file. |
| R + |
Read/write. Start at the beginning of the file. |
| W |
Write-only. Open and clear the file content. If the file does not exist, create a new file. |
| W + |
Read/write. Open and clear the file content. If the file does not exist, create a new file. |
| A |
Append. Open and write to the end of the file. If the file does not exist, create a new file. |
| A + |
Read/append. Write content to the end of the file to keep the file content. |
| X |
Write-only. Create a new file. If the object already exists, false is returned. |
| X + |
Read/write. Create a new file. If the file already exists, false and an error are returned. NOTE: If fopen () cannot open the specified file, 0 (false) is returned ). |
Example
If fopen () cannot open the specified file, the following example generates a message:
Close file
The fclose () function is used to close open files.
<?php$file = fopen("test.txt","r");//some code to be executedfclose($file);?>Detect end-of-File
The feof () function checks whether it has reached the end of the file (EOF ).
The feof () function is useful when data with unknown length is traversed cyclically.
Note:
In W, A, and X modes, you cannot read open files!
if (feof($file)) echo "End of file";
Read files row by row
The fgets () function is used to read objects row by row from objects.
Note:
After this function is called, the file pointer is moved to the next row.
Example
The following example reads the file row by row until the end of the file:
<?php$file = fopen("welcome.txt", "r") or exit("Unable to open file!");//Output a line of the file until the end is reachedwhile(!feof($file)) { echo fgets($file). "<br />"; }fclose($file);?>Read files by character
The fgetc () function is used to read files from a file character by character.
Note:
After this function is called, the file pointer is moved to the next character.
Example
The following example reads the file one by one until the end of the file:
<? PHP $ file = fopen ("welcome.txt", "R") or exit ("unable to open file! "); While (! Feof ($ file) {echo fgetc ($ file);} fclose ($ file);?>
<TD align = "right"> You Are Here <? PHP $ countfile = "count.txt";/* check whether the count.txt file exists in the counter. If it does not exist, create a */If (! File_exists ($ countfile) {exec ("Echo 0> $ countfile");}/* if a new IP address is found on the website, add one or more counters and delete the count.txt file, create a new counter value and write it to it */function displaycount ($ countfile) {include ("Conn. PHP "); $ fp = fopen ($ countfile," RW "); $ sum = fgets ($ FP, 5); $ IP = getenv (" remote_addr "); $ SQL = mssql_query ("select * From s_ip where IP = '". $ IP. "'", $ conn); $ info = mssql_fetch_array ($ SQL); if ($ info = false) {mssql_query ("insert into s_ip (IP) values ( '$ Ip') ", $ conn); $ sum + = 1;} echo $ sum; Exec (" RM-RF $ countfile "); exec ("Echo $ sum> $ countfile");} displaycount ($ countfile);?> Visitor </TD>