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

来源:互联网 发布:telnet不通公网80端口 编辑:程序博客网 时间:2024/05/17 22:50

读取文件

  • 先解释一下,什么是读取文件本身,什么叫读取文件输入内容。举个例子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
原创粉丝点击