Php tutorial fopen function usage (open a file)
In many times, we need to create files, especially when generating static pages. in php, we often use the fopen function to create files. It is used to append files when files exist, you can create a parameter if it does not exist. Many parameters are provided below. You can define parameters as needed.
$ Xxx = fopen ("text.txt", 'w', ture)
*/
$ Fopen = 'fopen.txt ';
$ Str = 'write a file www.bkjia.com ';
If (file_exists ($ fopen ))
{
$ H = fopen ($ fopen, 'a + ');
If (fwrite ($ h, $ str ))
{
Echo 'content added successfully ';
}
Fclose ();
}
Else
{
$ H = fopen ($ fopen, 'W + ');
If (fwrite ($ h, $ str ))
{
Echo 'file written successfully ';
}
Fclose ();
}
$ File = fopen ("test.txt", "r ");
$ File = fopen ("/home/mb.php100.com/test.txt", "r ");
$ File = fopen ("/home/test/test.gif", "wb ");
$ File = fopen ("http://www.bkjia.com/", "r ");
$ File = fopen ("ftp: // user: password@example.com/test.txt", "w ");
/*
Fopen function details
Definition and usage
The fopen () function opens a file or url.
If opening fails, this function returns false.
Syntax
Fopen (filename, mode, include_path, context) parameter description
Filename is required. Specifies the file or url to open.
Mode is required. Specifies the access type of the file/stream. Possible values are shown in the table below.
Optional. If you also need to retrieve the file in include_path, you can set this parameter to 1 or true.
Context is optional. Rules
Open the file in read-only mode and point the file pointer to the file header.
Open the "r +" read/write mode and point the file pointer to the file header.
Open the "w" Write mode, point the file pointer to the file header, and cut the file size to zero. Create.
Open in "w +" read/write mode, point the file pointer to the file header, and cut the file size to zero. Create.
"A" is opened in writing mode, pointing the file pointer to the end of the file. The file does not exist.
Open the "a +" read/write mode and point the file pointer to the end of the file. The file does not exist.
*/
?>