PHP使用fopen与file_get_contents读取文件实例分享_php实例
php中读取文件可以使用fopen和file_get_contents这两个函数,二者之间没有本质区别,只是前者读取文件的php代码相比后者要复杂一点。本文章通过实例向大家讲解fopen和file_get_contents读取文件的实现代码。需要的码农可以参考一下。
fopen读取文件的代码如下:
<?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); ?>
注意fopen读取文件需要配合使用fgets和fclose函数。
file_get_contents读取文件的代码如下:
<?php if (file_exists($path)) { $body = file_get_contents($path); echo $body; //输入文件内容 } else { echo "文件不存在 $path"; } ?>
这个函数是一次性读取所有文件内容并显示出来,但是如果文件超大会导致php占很大的内存了。
当然还有像file这种一般是把文件读成数组了,同时也可以实现读取文件了
下面给大家介绍下fopen()和file_get_contents()打开URL获得网页内容的用法区别
在php里,要想打开网页URL获得网页内容,比较常用的函数是fopen()和file_get_contents()。如果要求不苛刻,此两个函数多数情况下是可以根据个人爱好任意选择的,本文谈下此两函数的用法有什么区别,以及使用时需要注意的问题。
fopen()打开URL
下面是一个使用fopen()打开URL的例子:
<?php $fh = fopen('http://www.baidu.com/', 'r'); if($fh){ while(!feof($fh)) { echo fgets($fh); } } ?>
从此例子可以看到,fopen()打开网页后,返回的$fh不是字符串,不能直输出的,还需要用到fgets()这个函数来获取字符串。fgets()函数是从文件指针中读取一行。文件指针必须是有效的,必须指向由 fopen() 或 fsockopen() 成功打开的文件(并还未由 fclose() 关闭)。
可知,fopen()返回的只是一个资源,如果打开失败,本函数返回 FALSE 。
file_get_contents()打开URL
下面是一个使用file_get_contents()打开URL的例子:
<?php $fh= file_get_contents('http://www.baidu.com/'); echo $fh; ?>
从此例子看到,file_get_contents()打开网页后,返回的$fh是一个字符串,可以直接输出的。
通过上面两个例子的对比,可以看出使用file_get_contents()打开URL,也许是更多人的选择,因为其比fopen()更简单便捷。
不过,如果是读取比较大的资源,则是用fopen()比较合适。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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 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, 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. 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

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"){&

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