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

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

autoload
Release: 2023-03-09 14:10:01
Original
3482 people have browsed it

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 = ?   )
Copy after login
  • $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);

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template