Php uses the fopen function to create and open a file for details and instance code,
Php does not have a separate file to create a function. If you want to create a function, you can use the fopen () and fopen () functions to open the file, however, this function also provides the file creation function. When you use the fopen () function to open a file, if the file does not exist, you will try to create the file and return a resource.
Php fopen function Introduction
Fopen function to open a file or URL
Syntax:
Resource fopen (string filename, string mode)
Fopen () binds the name resource specified by filename to a stream.
Parameters:
1. filename is the name of the file to be opened/created.
If filename is in the format of "scheme: //...", it is treated as a URL. PHP will use the search protocol processor (also known as the Encapsulation Protocol) to process this mode. If the Protocol has not yet registered the encapsulation protocol, PHP will send a message to help check the potential problems in the script and continue executing the filename as a normal file name.
If PHP considers filename to be a local file, it will try to open a stream on the file. This file must be accessible by PHP. Therefore, you need to confirm that the file access permission permits this access. If the security mode or open_basedir is activated, further restrictions are applied.
If PHP considers filename to be a registered protocol registered as a web URL, PHP checks and confirms that allow_url_fopen is activated. If it is disabled, PHP will issue a warning, while the fopen call will fail.
2. mode specifies the open mode. The possible values are as follows:
Open Mode |
Description |
R |
Read-only, and point the file pointer to the starting position of the file |
R + |
Read/write: point the file pointer to the start position of the file. |
W |
Write-only: point the file pointer to the start position of the file and clear the file content. If the file does not exist, try to create it |
W + |
Read/write: point the file pointer to the start position of the file and clear the file content. If the file does not exist, try to create it |
A |
Append the object to the end of the object. If the object does not exist, try to create it. |
A + |
Read/write append: point the file pointer to the end of the file to operate. If the file does not exist, try to create |
X |
Write-only and create a file. If the file already exists, fopen () fails to be called and FALSE is returned. |
X + |
Read/write and create a file. If the file already exists, fopen () fails to be called and FALSE is returned. |
Php fopen function instance
1. Use the fopen function to create a file:
$ My_file = 'file.txt '; // if the file does not exist (in the current directory by default) $ handle = fopen ($ my_file, 'w') or die ('cannot open file :'. $ my_file); // implicitly creates file
2. Use the fopen function to open a file:
$ My_file = 'file.txt '; // The hypothetical file file.txt exists $ handle = fopen ($ my_file, 'w') or die ('cannot open file :'. $ my_file); // open file for writing ('w', 'R', 'A ')...
3. Use the fopen function with fread to read files:
$my_file = 'file.txt';$handle = fopen($my_file, 'r');$data = fread($handle,filesize($my_file));
4. Use the fopen function in combination with the fwrite function to write files
$my_file = 'file.txt';$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);$data = 'This is the data';fwrite($handle, $data);
5. The fopen function is combined with the fwrite function to Append content to the file:
$my_file = 'file.txt';$handle = fopen($my_file, 'a') or die('Cannot open file: '.$my_file);$data = 'New data line 1';fwrite($handle, $data);$new_data = "\n".'New data line 2';fwrite($handle, $new_data);
6. The fopen () function can also be used to open the URL address on the Internet:
<?php$fh = fopen("http://www.baidu.com/", "r");if($fh){ while(!feof($fh)) { echo fgets($fh); }}?>
Note: fopen () Only returns a resource. to display the address of the opened page, you must use the fgets () function to read and output the page.
I hope this article will help you. Thank you for your support for this site!