php读取文件 案例一 <div class="codetitle"><a style="CURSOR: pointer" data="27663" class="copybut" id="copybut27663" onclick="doCopy('code27663')"> 代码如下:<div class="codebody" id="code27663"> <?php $file = '52php.cn.php'; //本案例不支持远程 $fso = fopen($file,'r'); echo $data = fread($fso,filesize($file)); fclose($fso); ?> fopen() 将 file 指定的名字资源绑定到一个流上. filesize 返回文件大小的字节数,如果出错返回 FALSE. 注: 因为 PHP 的整数类型是有符号的,并且大多数平台使用 32 位整数,filesize() 函数在碰到大于 2GB 的文件时可能会返回非预期的结果.对于 2GB 到 4GB 之间的文件通常可以使用 sprintf("%u",filesize($file)) 来克服此问题. fread() 从文件指针 handle 读取最多 length 个字节. 该函数在读取完 length 个字节数,或到达 EOF 的时候,或(对于网络流)当一个包可用时就会停止读取文件,视乎先碰到哪种情况. 说明:低版本用法!建议php5用file_get_contents 案例二 <div class="codetitle"><a style="CURSOR: pointer" data="19377" class="copybut" id="copybut19377" onclick="doCopy('code19377')"> 代码如下:<div class="codebody" id="code19377"> <?php $file = '52php.cn.php'; //支持远程 $file = '//www.52php.cn';// echo $data = implode('',file($file)); ?> file -- 把整个文件读入一个数组中 说明 读取二进制的文件 案例三 <div class="codetitle"><a style="CURSOR: pointer" data="97351" class="copybut" id="copybut97351" onclick="doCopy('code97351')"> 代码如下:<div class="codebody" id="code97351"> <?php $file = '//www.52php.cn'; echo file_get_contents($file); ?> file_get_contents -- 将整个文件读入一个字符串 说明 string file_get_contents ( string filename [,int use_include_path [,resource context]]) 和 file() 一样,只除了 file_get_contents() 将文件返回为一个字符串. file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法.如果操作系统支持还会使用内存映射技术来增强性能. (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|