Home  >  Article  >  Backend Development  >  PHP uses fopen function to create and open files, detailed explanation and example code

PHP uses fopen function to create and open files, detailed explanation and example code

高洛峰
高洛峰Original
2016-12-21 15:48:212382browse

There is no separate file creation function in php. If we want to create a function, we can use fopen(). The fopen() function literally means to open a file, but this function also has the function of creating a file. When using the fopen() function to open a file , if the file does not exist, an attempt will be made to create the file and a resource will be returned.

php fopen function introduction

fopen function opens 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 you are trying to open/create.

If filename is in the format of "scheme://...", it is treated as a URL and PHP will search for a protocol handler (also called a wrapper protocol) to handle this pattern. If the wrapper protocol has not been registered for the protocol, PHP will emit a message to help check for potential problems in the script and continue execution of filename as if it were a normal filename.

If PHP thinks filename specifies a local file, it will try to open a stream on that file. The file must be accessible to PHP, so you need to confirm that the file access permissions allow this access. If safe mode or open_basedir is activated further restrictions apply.

If PHP thinks that filename specifies a registered protocol, and the protocol is registered as a network URL, PHP will check and confirm that allow_url_fopen has been activated. If it is closed, PHP will issue a warning and the call to fopen will fail.

2. mode specifies the opening mode, and its possible values ​​are as follows:

fopen函数创建 打开

php fopen function example

1. Use the fopen function to create a file:

$my_file = 'file.txt';//如果文件不存在(默认为当前目录下)
$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';//假设文件file.txt存在
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file); //open file for writing ('w','r','a')...

3. The fopen function is combined with fread to read the file:

$my_file = 'file.txt';
$handle = fopen($my_file, 'r');
$data = fread($handle,filesize($my_file));

4. The fopen function is combined with the fwrite function to write the file

$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 in file Additional content:

$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 a URL on the Internet. Address:

<?php
$fh = fopen("http://www.baidu.com/", "r");
if($fh){
  while(!feof($fh)) {
    echo fgets($fh);
  }
}
?>

Note: fopen() returns only a resource. To display the opened page The address also needs to be read and output using the fgets() function.

I hope this article can help everyone, thank you for your support of this site!

For more detailed explanations and example codes of using the fopen function to create and open files in PHP, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn