Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read a few lines of a large file by php

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how php reads a few lines of large files". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

php read a few lines of large files to achieve: 1, directly using the file function to operate;2, call Linux tail command to display the last few lines;3, use PHP fseek file operation;4, through PHP stream_get_line function to achieve reading.

Operating environment: Windows 7 system, PHP7.1 version, DELL G3 computer

How does PHP read a few lines in a large file?

PHP: How to read large files

Reading large files has always been a headache problem, we like to use php development to read small files can be achieved directly using various functions, but a large article will find that the commonly used method is not normal use or too long card, below we will take a look at the php read large file problem solution, I hope the example can help you.

In PHP, the quickest way to read files is to use functions such as file, file_get_contents, etc., which can be done beautifully with a few lines of code. However, when the file operated is a relatively large file, these functions may be insufficient. The following will start with a requirement to explain the common operation methods for reading large files.

Requirements:

There is an 800M log file, about 500 million lines, using PHP to return the last few lines of content.

How to do this:

1. Use the file function directly to operate

Because the file function reads everything into memory at once, PHP limits the maximum memory usage to 16M by default in order to prevent some poorly written programs from taking up too much memory and causing the server to crash. This is set by memory_limit = 16M in php.ini. If this value is set to-1, the memory usage is unlimited.

Here is a code that uses file to extract the last line of this file:

The entire code takes 116.9613 (s) to execute.

My machine is 2 G memory, when press F5 to run, the system directly gray, almost 20 minutes later to recover, visible will be such a large file directly read into memory, the consequences are how serious, so there is no last resort, memory_limit this thing can not be adjusted too high, otherwise only call the computer room, let reset the machine.

2. Call Linux tail command directly to display last lines

On the Linux command line, you can use tail -n 10 access.log to easily display the last few lines of the log file. You can call the tail command directly with PHP and execute the PHP code as follows:

The entire code takes 0.0034 (s) to execute.

3. Use PHP fseek directly for file manipulation

This method is the most common way, it does not need to read all the contents of the file into the content, but directly through the pointer to operate, so the efficiency is quite efficient. There are many different ways to operate on files using fseek, and the efficiency may vary slightly. Here are two commonly used methods:

method one

First find the last EOF of the file with fseek, then find the beginning of the last line, take that line, find the beginning of the next line, take that line, and so on until you find the $num line.

The implementation code is as follows

The entire code takes 0.0095 (s) to execute.

methodology II

Or use the fseek way to start reading from the end of the file, but this time is not a bit by bit read, but a piece of read, each read a piece of data, the read data will be placed in a buf, and then by the number of line change characters (\n) to determine whether the last $num lines of data have been read.

The implementation code is as follows

The entire code takes 0.0009(s) to execute.

method three

The entire code takes 0.0003(s) to execute.

Method 4, PHP stream_get_line function, read fast, read 500,000 data large files, about 20 seconds! The example code is as follows

$fp = fopen('./ iis.log', ' r'); //file while (! feof($fp)) { //for($j=1;$j

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report