Php uses the fopen function to create and open a file for details and instance code

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.