This article describes how to use the fopen function to create and open files in php and related information about the instance code. For more information, see the absence of a separate file creation function in php, if you want to create a function, you can use the fopen () and fopen () functions to open a file. However, this function also provides the file creation function () when the function opens a file, if the file does not exist, it 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:
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:
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!
For more information about how to create and open a file using the fopen function in php and related articles about the instance code, please follow the PHP Chinese network!