php使用fread原生读取文件实例demo源码

时间:2016-08-21
fread()从文件指针中读取最多M个字节,本文章向大家介绍php使用fread原生读取文件实例源码,需要的朋友可以参考一下。

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

语法:

string fread ( resource $handle , int $length )

fread() 从文件指针 handle 读取最多 length 个字节。 该函数在遇上以下几种情况时停止读取文件: 

  1. 读取了 length 个字节 
  2. 到达了文件末尾(EOF) 

参数:

  1. handle 文件系统指针,是典型地由 fopen() 创建的 resource(资源)。
  2. length 最多读取 length 个字节。 

比如现在有一个文件test.txt,其内容如下:

manongjc
php
css
java
mysql

现在我们使用fread函数读取这个文件里面的内容,源代码如下:

<?php
  $myfile = "./test.txt";

  $openfile = fopen ($myfile, "r") or die ("Couldn't open the file");
  $file_size=filesize($myfile);
  /* http://www.manongjc.com/article/1385.html */
  $file_content = fread ($openfile, $file_size);
  fclose ($openfile);

  echo $file_content;
?>

结果:

manongjc php css java mysql