Home  >  Article  >  Backend Development  >  Use of fopen() function in php (with code examples)

Use of fopen() function in php (with code examples)

autoload
autoloadOriginal
2021-05-08 12:30:563281browse

Use of fopen() function in php (with code examples)

The previous article "Analysis of the sprintf() function in php (with code examples)", this article introduces fopen() Function, php is a scripting language. In actual use, we may need to read some files and obtain their contents. At this time, we can use the fopen() function , this article will take you to take a look. First, let’s take a look at the syntax of fopen()

fopen ( string $filename   , string $mode   , bool $use_include_path = false   , resource $context = ?   )
  • $filename: the file or URL to be opened.

  • $mode: The parameter specifies the type of access required to the stream

  • $use_include_path: If you also need to search for files in include_path , can be set to '1' or true.

  • $context: Support for context content

  • Return value: Returns the file pointer resource on success. If the opening fails, this function returns false .

Code example:

<?php
$file=fopen("test.txt","r");

$user=array();
$i=0;
 //输出文本中所有的行,直到文件结束为止。
while(! feof($file))
 {
 $user[$i]= fgets($file);//fgets()函数从文件指针中读取一行
 $i++;
 }
 fclose($file);
 print_r($user);

?>
输出:
Array
(
    [0] => php.cn
)

Recommendation: 2021 PHP interview questions summary (collection) 》《php video tutorial

The above is the detailed content of Use of fopen() function in php (with code examples). For more information, please follow other related articles on 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