復(fù)制代碼 代碼如下:
$file=fopen("11.txt","r")or exit("Unable to open file!");//fopen打開文件,如果不存在就顯示打不開。
$filesize =filesize("11.txt");//計(jì)算文件大小
echo fread($file,$filesize);//讀取文件
fclose($file);//關(guān)閉文件
fopen()打開文件例子,
fclose()用不用在頁面上都沒有體現(xiàn),但是如果不用fclose()的話,被打開的文件會(huì)一直占用資源。
fopen() 打開網(wǎng)址例子:
復(fù)制代碼 代碼如下:
$web="http://www.baidu.com"; // http:// 不加的話就無法加載
$fp=fopen($web,'r');
if($fp){
while(!feof($fp)){
echo fgets($fp);
}
}
feof()檢查文件是否到末端 ,到末端返回1,沒有到返回0;
fgets()是逐行讀取。
file_get_contents()例子;
復(fù)制代碼 代碼如下:
$web ="http://www.baidu.com "
$fcontent=file_get_contents($web);
echo $fcontent;
顯然file_get_contents()更為簡(jiǎn)單。
而且在實(shí)驗(yàn)過程中我發(fā)現(xiàn),如果在 $web =""中 不加www. 會(huì)直接跳轉(zhuǎn),加www.會(huì)在本頁加載。