php 读取文件自身内容,与读取文件输出内容

读取文件

  • 先解释一下,什么是读取文件本身,什么叫读取文件输入内容。举个例子test.php里面的内容
<?php  
    echo "test"; 
?>

1,读取文件本身就是读取文件内所有内容,读取后就能得到(字符串)

<?php  
    echo "test"; 
?>

2,读取文件输出内容是读取文件所表现出来的东西,读取后得到test

fopen方法

1,读取文件本身

<?php  
    $filename = "test.php";  
    $handle = fopen($filename, "r");  
    $contents = fread($handle, filesize ($filename));  
    fclose($handle);  
    echo strlen($contents);  
?>  

2,读取文件输出内容

<?php  
    $filename = "http://localhost/test/test.php";  
    $handle = fopen($filename, "r");  
    $contents = "";  
    while (!feof($handle)) {  
         $contents .= fread($handle, 8192);  
    }  
    fclose($handle);  
    echo strlen($contents);  
?>  

上面二个读取的文件是同一个,但是为什么会不一样呢,http://localhost/test/test.php,在这里test.php文件被解释了,fopen只是得到这个脚本所输入的内容,看看php官方网站的解释吧
fopen() 将 filename 指定的名字资源绑定到一个流上。如果 filename 是 “scheme://…” 的格式,则被当成一个 URL,PHP 将搜索协议处理器(也被称为封装协议)来处理此模式。如果该协议尚未注册封装协议,PHP 将发出一条消息来帮助检查脚本中潜在的问题并将 filename 当成一个普通的文件名继续执行下去。

file方法

1,读取文件本身

<?php  
    $filename = "test.php";  
    $content = file($filename);                 //得到数组  
    print_r($content);  
?>  

2,读取文件显示输出内容

<?php
    $filename = "http://localhost/test/test.php";
    $content = file($filename);
    print_r($content);
?>

4,file_get_contents方法

1,读取文件本身

<?php  
    $filename = "test.php";  
    $content = file_get_contents($filename);     //得到字符串  
    echo strlen($content);  
?>  

2,读取文件显示输出内容

<?php
    $filename = "http://localhost/test/test.php";
    $content = file_get_contents($filename);
    echo strlen($content);
?>

5,readfile方法

(1),读取文件本身

<?php  
    $filename = "test.php";  
    $num = readfile($filename);     //返回字节数  
    echo $num;  
?> 

(2),读取文件显示输出内容

<?php  
    $filename = "http://localhost/test/test.php";  
    $num = readfile($filename);     //返回字节数  
    echo $num;  
?>  

6,ob_get_contents方法

1,读取文件显示输出内容

<?php  
    ob_start();  
    require_once('bbb.php');  
    $content = ob_get_contents();  
    ob_end_clean();  
    echo strlen($content);  
?>  

总结
php,读取文件的方法很多,读取url的方法也很多,个人总结了一下,如有不对请大家指正,如果有不足请大家补充。

作者:海底苍鹰

原文:http://blog.51yip.com/php/707.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值