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

相關文章
相關標籤/搜索