Home Daily Programming PHP Knowledge How to read file content in PHP? (Pictures + Videos)

How to read file content in PHP? (Pictures + Videos)

Sep 18, 2018 pm 05:05 PM

This article mainly introduces two methods of using PHP to read file content.

In the previous article [How does PHP write data to a specified file? ] has explained to you the specific method of writing files in PHP. In fact, the operation methods of writing and reading PHP files are similar. If you need it, you can study this article first.

Below we will introduce in detail the two methods of reading file content in PHP.

The first method: fread function

<?php
$file=fopen(&#39;1.txt&#39;,&#39;rb+&#39;);
echo fread($file,filesize(&#39;1.txt&#39;));
fclose($file);
Copy after login

Here we first open the 1.txt file through fopen, and then use fread Function Read the contents of txt file.

Note: The first parameter in fread represents the file read, and the second parameter represents the length of the read file.

If we want to read all the contents of the file, we need to use the filesize function to get all the bytes of the file.

Then the result of reading the file content by this method is as follows:

How to read file content in PHP? (Pictures + Videos)

How to read file content in PHP? (Pictures + Videos)

As you can see from the picture The read content is the same as the original file content.

Second method: file_get_contents function

echo  file_get_contents(&#39;1.txt&#39;);
Copy after login

This method is very simple. We directly use the file_get_contents function to read the local file content.

Note: The file_get_contents() function is the preferred method for reading the contents of a file into a string.

This article is about two methods of reading the contents of txt files in PHP. If you want to learn more about PHP, you can follow the PHP video tutorial on the PHP Chinese website. Everyone is welcome to refer to and learn!

The above is the detailed content of How to read file content in PHP? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months 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)