Home Backend Development PHP Tutorial PHP使用fopen与file_get_contents读取文件实例分享_php实例

PHP使用fopen与file_get_contents读取文件实例分享_php实例

Jun 07, 2016 pm 05:09 PM
file_get_contents php read file

php中读取文件可以使用fopen和file_get_contents这两个函数,二者之间没有本质区别,只是前者读取文件的php代码相比后者要复杂一点。本文章通过实例向大家讲解fopen和file_get_contents读取文件的实现代码。需要的码农可以参考一下。

fopen读取文件的代码如下:

<&#63;php
$file_name = "1.txt";
echo $file_name . "
";
$fp = fopen($file_name, 'r');
//$buffer=fgets($fp);
while (!feof($fp)) {
$buffer = fgets($fp);
echo $buffer;
}
fclose($fp);
&#63;> 
Copy after login

注意fopen读取文件需要配合使用fgets和fclose函数。

file_get_contents读取文件的代码如下:

<&#63;php
if (file_exists($path)) {
$body = file_get_contents($path);
echo $body; //输入文件内容
} else {
echo "文件不存在 $path";
}
&#63;> 
Copy after login

这个函数是一次性读取所有文件内容并显示出来,但是如果文件超大会导致php占很大的内存了。

当然还有像file这种一般是把文件读成数组了,同时也可以实现读取文件了

下面给大家介绍下fopen()和file_get_contents()打开URL获得网页内容的用法区别

在php里,要想打开网页URL获得网页内容,比较常用的函数是fopen()和file_get_contents()。如果要求不苛刻,此两个函数多数情况下是可以根据个人爱好任意选择的,本文谈下此两函数的用法有什么区别,以及使用时需要注意的问题。

fopen()打开URL

下面是一个使用fopen()打开URL的例子:

<&#63;php
$fh = fopen('http://www.baidu.com/', 'r');
if($fh){
while(!feof($fh)) {
echo fgets($fh);
}
}
&#63;>
Copy after login

从此例子可以看到,fopen()打开网页后,返回的$fh不是字符串,不能直输出的,还需要用到fgets()这个函数来获取字符串。fgets()函数是从文件指针中读取一行。文件指针必须是有效的,必须指向由 fopen() 或 fsockopen() 成功打开的文件(并还未由 fclose() 关闭)。

可知,fopen()返回的只是一个资源,如果打开失败,本函数返回 FALSE 。

file_get_contents()打开URL

下面是一个使用file_get_contents()打开URL的例子:

<&#63;php
$fh= file_get_contents('http://www.baidu.com/');
echo $fh;
&#63;>
Copy after login

从此例子看到,file_get_contents()打开网页后,返回的$fh是一个字符串,可以直接输出的。

通过上面两个例子的对比,可以看出使用file_get_contents()打开URL,也许是更多人的选择,因为其比fopen()更简单便捷。

不过,如果是读取比较大的资源,则是用fopen()比较合适。

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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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: 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 PHPWarning: file_get_contents(): Filenamecannotbeempty In the process of PHP development, we often encounter this error message: PHPWarning: file_get_contents(): Filenamecannotbeempty. This error usually occurs when using the file_get_contents function

如何解决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

How to solve PHPWarning:file_get_contents():failedtoopenstream:HTTPrequestfailed During PHP development, we often encounter situations where HTTP requests are initiated to remote servers through the file_get_contents function. However, sometimes we encounter a common error message: PHPWarning: file_get_c

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 content from a file, specific code example In PHP, file_get_contents() is a very useful function that allows us to read content from a file. Whether reading a text file or reading content from a remote URL, this function can easily complete the task. Syntax The basic syntax of this function is as follows: stringfile_get_contents(string$f

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, which require specific code examples. In web development, we often need to read data from files or write data to files. Moreover, in some cases, we need to cache the contents of files to avoid frequent file read and write operations, thus improving performance. In PHP, there are several commonly used functions that can help us implement file caching, including

PHP function introduction—file_get_contents(): Read the contents of the URL into a string PHP function introduction—file_get_contents(): Read the contents of the URL into a string Jul 24, 2023 pm 02:32 PM

PHP function introduction—file_get_contents(): Read the contents of the URL into a string. In web development, it is often necessary to obtain data from a remote server or read a remote file. PHP provides a very powerful function file_get_contents(), which can conveniently read the contents of a URL and save it to a string. This article will introduce the usage of file_get_contents() function and give some code examples to help readers better

Read last line of file in PHP Read last line of file in PHP Aug 27, 2023 pm 10:09 PM

To read the last line of a file from PHP, the code is as follows -$line='';$f=fopen('data.txt','r');$cursor=-1;fseek($f,$cursor, SEEK_END);$char=fgetc($f);//Trimtrailingnewlinecharactersinthefilewhile($char===""||$char==="\r"){&

How to read file contents using file_get_contents function in PHP How to read file contents using file_get_contents function in PHP Jun 26, 2023 pm 12:01 PM

In PHP, we often need to read data from files. In this case, we can use the file_get_contents function. This function can simply read everything from a file and return it as a string. This is very useful in many scenarios, such as reading configuration files, reading log files, etc. In this article, we will explain how to read file contents using the file_get_contents function in PHP. Step 1: Open the file using file

Detailed explanation of PHP 5.2 functions: How to use the file_get_contents function to read file contents Detailed explanation of PHP 5.2 functions: How to use the file_get_contents function to read file contents Jul 29, 2023 pm 04:09 PM

Detailed explanation of PHP5.2 functions: How to use the file_get_contents function to read file contents. In PHP development, we often need to read the contents of files. PHP provides many methods to read file contents. One of the commonly used and powerful functions is file_get_contents(). This function can read the content from a file and return the content in the form of a string to facilitate our subsequent processing. file_get_contents() function

See all articles