search
Homephp教程PHP源码php读取文本文件内容

在php语言中读取文件的方法很多,今天我们就来讲三种php读取文本文件内容实例吧,主要是用到fopen,file,file_get_contents函数来实现。

<script>ec(2);</script>

//fopen 读取文件实例

 代码如下 复制代码

$path ='a.txt';
$fp=fopen($file,"r");//以只读的方式打开文件
while(!(feof($fp)))
{
 $text=fgets($fp);//读取文件的一行
 echo $text;    
}

//file_get_contents读取文件

 代码如下 复制代码

if( file_exists( $path ) )
{
    $body = file_get_contents($path);
 echo $body ;//输入文件内容
}
else
{
    echo "文件不存在 $path";
}

//file

读取文本文件

 代码如下 复制代码
$cbody = file($path);


print_r($cbody); //因为file读取出来的文件是以数组形式保存的,所以用print_r输出。

 

Statement
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft