Home Backend Development PHP Tutorial PHP fopen()和 file_get_contents()应用与差异介绍_php技巧

PHP fopen()和 file_get_contents()应用与差异介绍_php技巧

May 17, 2016 am 08:47 AM
file_get_contents fopen

复制代码 代码如下:

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

fopen()打开文件例子,

fclose()用不用在页面上都没有体现,但是如果不用fclose()的话,被打开的文件会一直占用资源。
fopen() 打开网址例子:
复制代码 代码如下:

$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()例子;
复制代码 代码如下:

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

显然file_get_contents()更为简单。

而且在实验过程中我发现,如果在 $web =""中 不加www. 会直接跳转,加www.会在本页加载。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve PHP Warning: fopen(): failed to open stream: No such file or directory How to solve PHP Warning: fopen(): failed to open stream: No such file or directory Aug 19, 2023 am 10:44 AM

How to solve PHP Warning: fopen(): failed to open stream: No such file or directory

如何解决PHP Warning: file_get_contents(): failed to open stream: HTTP request failed 如何解决PHP Warning: file_get_contents(): failed to open stream: HTTP request failed Aug 18, 2023 pm 11:34 PM

如何解决PHP Warning: file_get_contents(): failed to open stream: HTTP request failed

How to solve PHP Warning: file_get_contents(): Filename cannot be empty How to solve PHP Warning: file_get_contents(): Filename cannot be empty Aug 18, 2023 pm 07:30 PM

How to solve PHP Warning: file_get_contents(): Filename cannot be empty

How to solve PHP Warning: fopen(): SSL operation failed in file.php on line X How to solve PHP Warning: fopen(): SSL operation failed in file.php on line X Aug 25, 2023 am 09:22 AM

How to solve PHP Warning: fopen(): SSL operation failed in file.php on line X

How to solve PHP Warning: fopen(): failed to open stream: Permission denied How to solve PHP Warning: fopen(): failed to open stream: Permission denied Aug 20, 2023 pm 01:45 PM

How to solve PHP Warning: fopen(): failed to open stream: Permission denied

Usage of fopen function in Matlab Usage of fopen function in Matlab Nov 28, 2023 am 11:03 AM

Usage of fopen function in Matlab

PHP's file_get_contents() function: How to read contents from a file PHP's file_get_contents() function: How to read contents from a file Nov 04, 2023 pm 01:43 PM

PHP's file_get_contents() function: How to read contents from a file

Detailed explanation of PHP file caching functions: file caching processing methods of file_get_contents, file_put_contents, unlink and other functions Detailed explanation of PHP file caching functions: file caching processing methods of file_get_contents, file_put_contents, unlink and other functions Nov 18, 2023 am 09:37 AM

Detailed explanation of PHP file caching functions: file caching processing methods of file_get_contents, file_put_contents, unlink and other functions

See all articles