fread()、fgets()、fgetc()、file_get_contents() 与 file() 函数用于从文件中读取内容。

  1.fread()

  fread()函数用于读取文件(可安全用于二进制文件)

  语法:string fread(int handle,int length)

  fread() 从文件指针 handle 读取最多 length 个字节。当遇到下列任何一种情况时,会停止读取文件

  1.在读取完最多 length 个字节数时

  2.达到文件末尾的时候(EOF)

   3.(对于网络流)当一个包可用时

   4.(在打开用户空间流之后)已读取了 8192 个字节时.                       例如:从文件test.txt中读取10个字节。

<?php
$filename = "test.txt";
$fh = fopen($filename, "r");
echo fread($fh, "10");
fclose($fh);
?>

  2.fegts()

  fgets() 函数用于从文件中读取 一行 数据,并将文件指针指向下一行。提示:如果想在读取的时候去掉文件中的 HTML 标记,请使用 fgetss() 函数。

  语法:string fgets( int handle [,int length] )

  fgets() 从 handle 指向的文件中读取一行并返回长度最多为 length-1 字节的字符串。碰到换行符(包括在返回值中)、EOF 或者已经读取了 length-1 字节后停止。如果没有指定 length ,则默认为 1K ,或者说 1024 字节。

<?php
$fh = @fopen("test.txt","r") or die("打开 test.txt 文件出错!");
// if条件避免无效指针
if($fh){
while(!feof($fh)) {
echo fgets($fh), '<br />';
}
}
fclose($fh);
?>

  3.fgetc()

  fgetc()函数用于逐字读取文件数据,直到文件结束。

  语法:string fgetc( resource handle )

  

<?php
$fh = @fopen("test.txt","r") or die("打开 test.txt 文件出错!");
if($fh){
while(!feof($fh)) {
echo fgetc($fh);
}
}
fclose($fh);
?>

  4.file_get_contents()

  file_get_contents() 函数用于把 整个文件 读入一个字符串,成功返回一个字符串,失败则返回 FALSE。

  语法:string file_get_contents( string filename [, int offset [, int maxlen] ] )

  filename:要读取的文件名称

  offset:可选,指定读取位置,默认从文件开始位置

  maxlen:可选,指定读取文件的长度,单位字节

<?php
// 读取时同事将换行符转换成 <br />
echo nl2br(file_get_contents('test.txt'));
?>

  5.file()

  file()函数用于把整个文件读入一个数组中,数组中的每个单元都是文件中相应的一行,包括换行符在内。成功返回一个数组,失败则返回 FALSE。

  语法:array file( string filename )

 

<?php
$lines = file('test.txt');
// 在数组中循环并加上行号
foreach ($lines as $line_num => $line) {
echo "Line #{$line_num} : ",$line,'<br />';
}
?>

  test.txt文件内容:

      你好!

      这是第二行文字。

  输出:

      line #0:你好!

      line #1:这是第二行文字。

 

PHP读取文件函数fread,fgets,fgetc,file_get_contents和file函数的使用总结的更多相关文章

  1. PHP 文件读取 fread、fgets、fgetc、file_get_contents 与 file 函数

    fread().fgets().fgetc().file_get_contents() 与 file() 函数用于从文件中读取内容. fread() fread() 函数用于读取文件(可安全用于二进制 ...

  2. 读取文件内容fopen,fgets,fclose

    <?php //首先采用“fopen”函数打开文件,得到返回值的就是资源类型.$file_handle = fopen("/data/webroot/resource/php/f.tx ...

  3. PHP文件操作,多行句子的读取,file()函数,file_get_contents()函数,file_put_contents()函数,is_file,统计网站pv (访问量),文件的复制 copy,文件重命名 rename,删除文件 unlink

    php中添加utf-8: header("Content-type:text/html;charset='UTF-8'"); 文件操作步骤: 1.在同一目录下建立一个file.tx ...

  4. php学习笔记--高级教程--读取文件、创建文件、写入文件

    打开文件:fopen:fopen(filename,mode);//fopen("test.txt","r"): 打开模式:r  仅仅读方式打开,将文件指针指向 ...

  5. php读取文件内容的三种方法

    <?php //**************第一种读取方式***************************** 代码如下: header("content-type:text/h ...

  6. PHP读取文件内容的三种方式

    <?php // 第一种读取方式 header("content-type:text/html;charset=utf-8"); // 文件路径 $fileA = " ...

  7. C++/Php/Python/Shell 程序按行读取文件或者控制台

    写程序经常需要用到从文件或者标准输入中按行读取信息,这里汇总一下.方便使用 1. C++ 读取文件 #include<stdio.h> #include<string.h> i ...

  8. C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结。

    C++/Php/Python/Shell 程序按行读取文件或者控制台方法总结. 一.总结 C++/Php/Python/Shell 程序按行读取文件或者控制台(php读取标准输入:$fp = fope ...

  9. 封装读取文件(node js)

    我们都会简单的读取文件,今天我们就来讲一下用函数封装读取文件. 1.首先我们要先建好文件 2.我们在index.js里面写入代码: var http=require('http'); var fs=r ...

随机推荐

  1. Mysql5.7 用户与授权

    mysql -uroot -proot MySQL5.7 mysql.user表没有password字段改 authentication_string: 一. 创建用户: 命令:CREATE USER ...

  2. Apache 错误整理

    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localho ...

  3. 行列转换文本处理--awk xargs 回顾

    awk 数组回顾: 9.1 数组 举例:统计当前主机上每一个TCP连接状态以及每种连接状态的数目[非常实用] # netstat -tan | awk '/^tcp/{STATE[$NF]++}END ...

  4. Metasploit 内网渗透篇

    0x01 reverse the shell File 通常做法是使用msfpayload生成一个backdoor.exe然后上传到目标机器执行.本地监听即可获得meterpreter shell. ...

  5. 并发-CopyOnWrite源码分析

    CopyOnWrite源码分析 参考: https://blog.csdn.net/linsongbin1/article/details/54581787 http://ifeve.com/java ...

  6. Android -- junit测试框架,logcat获取log信息

    1. 相关概念 白盒测试: 知道程序源代码. 根据测试的粒度分为不同的类型   方法测试 function test         单元测试 unit test                 集成 ...

  7. LeetCode第[69]题(Java):Sqrt(x)

    题目:求平方根 难度:Easy 题目内容: Compute and return the square root of x, where x is guaranteed to be a non-neg ...

  8. angularjs地址栏传参

    1:路由定义参数 2.controller 3. 4.目标得到参数值

  9. canvas画的北斗七星和大熊座

    用canvas画的北斗七星和大熊座,主要用到的知识点是:canvas.定时器. html代码: <body> <canvas id="canvas" width= ...

  10. 智课雅思词汇---二十五、-ate

    智课雅思词汇---二十五.-ate 一.总结 一句话总结:又是动词,又是名词,又是形容词 后缀:-ate ①[动词后缀] 表示做.造成.使之成....做...事等意义 hyphenate 加连字符 o ...