1 $file = fopen ( 11.txt , r ) or exit ( Unabletoopenfile! ); //fopen打开文件,如果不存在就显示打不开。 2 $filesize = filesize ( 11.txt ); //计算文件大小 3 echo fread ( $file , $filesize ); //读取文件 4 fclose( $file ); //关闭文件 fopen(
1
|
$file=fopen("11.txt","r")or exit("Unable to open file!");//fopen打开文件,如果不存在就显示打不开。
|
2
|
$filesize =filesize("11.txt");//计算文件大小
|
3
|
echo fread($file,$filesize);//读取文件
|
4
|
fclose($file);//关闭文件
|
fopen()打开文件例子,
fclose()用不用在页面上都没有体现,但是如果不用fclose()的话,被打开的文件会一直占用资源。
fopen ( )打开网址例子:
1
|
$web="http://www.baidu.com"; // http:// 不加的话就无法加载
|
2
|
$fp=fopen($web,'r');
|
3
|
if($fp){
|
4
|
while(!feof($fp)){
|
5
|
echo fgets($fp);
|
6
|
}
|
7
|
}
|
feof()检查文件是否到末端 ,到末端返回1,没有到返回0;
fgets()是逐行读取。
file_get_contents()例子;
1
|
$web ="http://www.baidu.com " |
2
|
$fcontent=file_get_contents($web);
|
3
|
echo $fcontent;
|
显然file_get_contents()更为简单。
而且在实验过程中我发现,如果在 $web =""中 不加www. 会直接跳转,加www.会在本页加载。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号