PHP fopen()和 file_get_contents() 应用与差别。

$file=fopen("11.txt","r")or exit("Unable to open file!");//fopen打开文件,若是不存在就显示打不开。
    $filesize =filesize("11.txt");//计算文件大小
    echo fread($file,$filesize);//读取文件
    fclose($file);//关闭文件

fopen()打开文件例子,php

fclose()用不用在页面上都没有体现,可是若是不用fclose()的话,被打开的文件会一直占用资源。web



fopen (  )打开网址例子:code

$web="http://www.baidu.com";    // http:// 不加的话就没法加载
$fp=fopen($web,'r');
    if($fp){
    while(!feof($fp)){
    echo fgets($fp);
    }
    }

feof()检查文件是否到末端  ,到末端返回1,没有到返回0;资源

fgets()是逐行读取。get


file_get_contents()例子;it

 $web ="http://www.baidu.com "    
    $fcontent=file_get_contents($web);
    echo $fcontent;

 显然file_get_contents()更为简单。class

并且在实验过程当中我发现,若是在 $web =""中 不加www. 会直接跳转,加www.会在本页加载。file

相关文章
相关标签/搜索