search
HomeBackend DevelopmentPHP TutorialPHP uses fopen and file_get_contents to read file instances

To read files in PHP, you can use the two functions fopen and file_get_contents. There is no essential difference between the two, except that the PHP code for reading files in the former is a little more complicated than the latter. This article explains the implementation code of fopen and file_get_contents to read files through examples. Coders who need it can refer to it.

The code for fopen to read files is as follows:

<span>php
</span><span>$file_name</span> = "1.txt"<span>;
</span><span>echo</span><span>$file_name</span> . "
"<span>;
</span><span>$fp</span> = <span>fopen</span>(<span>$file_name</span>, 'r'<span>);
</span><span>//</span><span>$buffer=fgets($fp);</span><span>while</span> (!<span>feof</span>(<span>$fp</span><span>)) {
    </span><span>$buffer</span> = <span>fgets</span>(<span>$fp</span><span>);
    </span><span>echo</span><span>$buffer</span><span>;
}
</span><span>fclose</span>(<span>$fp</span><span>);
</span>?>       

Note that fopen needs to use the fgets and fclose functions to read files.

file_get_contents The code for reading files is as follows:

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

This function reads all the file contents at once and displays them, but if the file is too large, PHP will occupy a lot of memory.

Of course, there are also methods like file, which generally read files into arrays. At the same time, files can also be read

The above introduces the example of using fopen and file_get_contents to read files in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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
鸿蒙原生应用随机诗词鸿蒙原生应用随机诗词Feb 19, 2024 pm 01:36 PM

想了解更多关于开源的内容,请访问:51CTO鸿蒙开发者社区https://ost.51cto.com运行环境DAYU200:4.0.10.16SDK:4.0.10.15IDE:4.0.600一、创建应用点击File->newFile->CreateProgect。选择模版:【OpenHarmony】EmptyAbility:填写项目名,shici,应用包名com.nut.shici,应用存储位置XXX(不要有中文,特殊字符,空格)。CompileSDK10,Model:Stage。Device

php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

使用java的File.length()函数获取文件的大小使用java的File.length()函数获取文件的大小Jul 24, 2023 am 08:36 AM

使用Java的File.length()函数获取文件的大小文件大小是在处理文件操作时很常见的一个需求,Java提供了一个很方便的方法来获取文件的大小,即使用File类的length()方法。本文将介绍如何使用该方法来获取文件的大小,并给出相应的代码示例。首先,我们需要创建一个File对象来表示我们想要获取大小的文件。以下是创建File对象的方法:Filef

php blob怎么转filephp blob怎么转fileMar 16, 2023 am 10:47 AM

php blob转file的方法:1、创建一个php示例文件;2、通过“function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })}”方法实现Blob转File即可。

如何解决PHP Warning: fopen(): failed to open stream: No such file or directory如何解决PHP Warning: fopen(): failed to open stream: No such file or directoryAug 19, 2023 am 10:44 AM

如何解决PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory在使用PHP开发过程中,我们经常会遇到一些文件操作的问题,其中之一就是"PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory

使用java的File.renameTo()函数重命名文件使用java的File.renameTo()函数重命名文件Jul 25, 2023 pm 03:45 PM

使用Java的File.renameTo()函数重命名文件在Java编程中,我们经常需要对文件进行重命名的操作。Java提供了File类来处理文件操作,其中的renameTo()函数可以方便地重命名文件。本文将介绍如何使用Java的File.renameTo()函数来重命名文件,并提供相应的代码示例。File.renameTo()函数是File类的一个方法,

如何解决PHP Warning: fopen(): SSL operation failed in file.php on line X如何解决PHP Warning: fopen(): SSL operation failed in file.php on line XAug 25, 2023 am 09:22 AM

如何解决PHPWarning:fopen():SSLoperationfailedinfile.phponlineX在PHP编程中,我们经常使用fopen函数来打开文件或者URL,并进行相关操作。然而,在使用fopen函数时,有时候会遇到类似于Warning:fopen():SSLoperationfailedinfile.p

如何解决PHP Warning: fopen(): failed to open stream: Permission denied如何解决PHP Warning: fopen(): failed to open stream: Permission deniedAug 20, 2023 pm 01:45 PM

如何解决PHPWarning:fopen():failedtoopenstream:Permissiondenied在开发PHP程序的过程中,我们常常会遇到一些报错信息,比如PHPWarning:fopen():failedtoopenstream:Permissiondenied。这个错误通常是由于文件或目录权限不正

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.