好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

php中fopen()函数的使用(附代码示例)

上一篇文章《解析php中sprintf()函数(附代码实例)》,本文介绍的是 fopen() 函数, php 是脚本语言,在实际使用过程中,我们可能需要读取一些文件,获取其中的内容,这时候我们就可以是使用 fopen() 这个函数,本文就带大家一起来看一看。首先来看一看 fopen() 的语法

fopen ( string $filename   , string $mode   , bool $use_include_path = false   , resource $context = ?   )

$filename:要打开的文件或 URL。

$mode:参数指定了所要求到该流的访问类型

$use_include_path:如果也需要在include_path中搜寻文件的话,可以将设为 '1' 或 true。

$context:对上下文内容的支持

返回值:成功时返回文件指针资源,如果打开失败,本函数返回 false。

代码实例:

<?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
)

推荐: 《 2021年PHP面试题大汇总(收藏) 》《 php视频教程 》

以上就是php中fopen()函数的使用(附代码示例)的详细内容!

查看更多关于php中fopen()函数的使用(附代码示例)的详细内容...

  阅读:47次