The fopen() function opens a file or URL.
If fopen() fails, it returns FALSE with an error message. You can hide error output by adding an '@' in front of the function name.
fopen(filename,mode,include_path,context)
| parameter | describe |
|---|---|
| filename | Required. Specifies the file or URL to open. |
| mode | Required. Specifies the type of access you are requesting to this file/stream. Possible values: "r" (open in read-only mode, point the file pointer to the file header) "r+" (open in read-write mode, point the file pointer to the file header) "w" (open for writing, clear the file contents, try to create the file if it does not exist) "w+" (open in read-write mode, clear the file contents, and try to create the file if it does not exist) "a" (open in writing mode, point the file pointer to the end of the file to write, if the file does not exist, try to create it) "a+" (open in read-write mode, save the file content by pointing the file pointer to the end of the file and writing) "x" (creates a new file and opens it for writing, or returns FALSE and an error if the file already exists) "x+" (creates a new file and opens it for reading and writing, or returns FALSE and an error if the file already exists) |
| include_path | Optional. Set this parameter to '1' if you also want to search for files in include_path (in php.ini). |
| context | Optional. Specifies the environment for a file handle. context is a set of options that can modify the behavior of the stream. |
NOTE: When writing a text file, make sure you use the correct line terminators! On Unix systems, the line terminator is n; on Windows systems, the line terminator is rn; on Macintosh systems, the line terminator is r. Windows systems provide a text conversion tag "t" that can transparently convert n to rn. You can also use "b" to force binary mode so the data is not converted. To use these flags, use "b" or "t" as the last character of the mode parameter.
<?php$file = fopen("test.txt","r");$file = fopen("/home/test/test.txt","r");$file = fopen("/home/test /test.gif","wb");$file = fopen("http://www.example.com/","r");$file = fopen("ftp://user:[email protected]/test.txt","w");?>