php读出文件,PHP读取文件

PHP提供了从文件读取数据的各种功能(函数)。 可使用不同的函数来读取所有文件数据,逐行读取数据和字符读取数据。

下面给出了可用的几种PHP文件读取函数。

fread()

fgets()

fgetc()

PHP读取文件 - fread()

PHP fread()函数用于读取文件的数据。 它需要两个参数:文件资源($handle)和文件大小($length)。

语法

string fread (resource $handle , int $length )

$handle表示由fopen()函数创建的文件指针。

$length表示要读取的字节长度。

示例

$filename = "c:\\file1.txt";

$fp = fopen($filename, "r");//open file in read mode

$contents = fread($fp, filesize($filename));//read file

echo "

$contents
";//printing data of file

fclose($fp);//close file

?>

上面代码执行结果如下 -

this is first line

this is another line

this is third line

PHP读取文件 - fgets()函数

PHP fgets()函数用于从文件中读取单行数据内容。

语法

string fgets ( resource $handle [, int $length ] )

示例

$fp = fopen("c:\\file1.txt", "r");//open file in read mode

echo fgets($fp);

fclose($fp);

?>

上面代码输出结果如下 -

this is first line

PHP读取文件 - fgetc()函数

PHP fgetc()函数用于从文件中读取单个字符。 要使用fgetc()函数获取所有数据,请在while循环中使用!feof()函数作为条件。

语法

string fgetc ( resource $handle )

示例

$fp = fopen("c:\\file1.txt", "r");//open file in read mode

while(!feof($fp)) {

echo fgetc($fp);

}

fclose($fp);

?>

上面代码输出结果如下 -

this is first line this is another line this is third line

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值