php read file line by line

Read a file backwards line by line using fseek

How do I read a file backwards line by line using fseek?

code can be helpful. must be cross platform and pure php.

many thanks in advance

9 Answers 9

If you are going to read the entire file in anyways, just use file() to read the file in as an array (each line is each element in the array) and then use array_reverse() to flip the array backwards and loop through that. Or just do a reverse for loop where you start at the end and decrement on each loop.

The question is asking using fseek, so can only assume that performance is an issue and file() is not the solution. Here is a simple approach using fseek:

To completely reverse a file:

Of course, you wanted line-by-line reversal.

Really though, Jonathan Kuhn has the best answer IMHO above. The only cases you’d not use his answer that I know of is if file or like functions are disabled via php.ini, yet the admin forgot about fseek, or when opening a huge file just get the last few lines of contents would magically save memory this way.

Note: Error handling not included. And, PHP_EOL didn’t cooperate, so I used «\n» to denote end of line instead. So, above may not work in all cases.

You cannot fseek line by line, because you do not know how long the lines are until you read them.

You should either read the whole file into a list of lines, or if the file is too big for that and you only need the last lines, read fixed-sized chunks from the end of the file and implement a bit more complicated logic which detects lines from such data.

Reading the entire file into an array and reversing is fine unless the file is enormous.

You could perform a buffered read of your file from back to front with something like this:

Источник

How to read a large file line by line?

I want to read a file line by line, but without completely loading it in memory.

My file is too large to open in memory, and if try to do so I always get out of memory errors.

The file size is 1 GB.

14 Answers 14

You can use the fgets() function to read the file line by line:

php read file line by line. Смотреть фото php read file line by line. Смотреть картинку php read file line by line. Картинка про php read file line by line. Фото php read file line by line

php read file line by line. Смотреть фото php read file line by line. Смотреть картинку php read file line by line. Картинка про php read file line by line. Фото php read file line by line

If you’re opening a big file, you probably want to use Generators alongside fgets() to avoid loading the whole file into memory:

This way you can process individual file lines inside the foreach().

Note: Generators require >= PHP 5.5

php read file line by line. Смотреть фото php read file line by line. Смотреть картинку php read file line by line. Картинка про php read file line by line. Фото php read file line by line

There is a file() function that returns an array of the lines contained in the file.

php read file line by line. Смотреть фото php read file line by line. Смотреть картинку php read file line by line. Картинка про php read file line by line. Фото php read file line by line

Use buffering techniques to read the file.

The obvious answer wasn’t there in all the responses.
PHP has a neat streaming delimiter parser available made for exactly that purpose.

This how I manage with very big file (tested with up to 100G). And it’s faster than fgets()

php read file line by line. Смотреть фото php read file line by line. Смотреть картинку php read file line by line. Картинка про php read file line by line. Фото php read file line by line

SplFileObject is useful when it comes to dealing with large files.

Источник

How to efficiently read and parse a huge CSV file line by line in PHP

Read this article in other language

If you work for a company that offers development services even for other companies of the same industry, you may have had this «wonderful» task of importing a huge «database» from a client into the database engine preferred by your company. For example, in our company, we work with MySQL and our client came up with a CSV file of approximately 25GB with

php read file line by line. Смотреть фото php read file line by line. Смотреть картинку php read file line by line. Картинка про php read file line by line. Фото php read file line by line

Obviously, because of the logic that the project required, we couldn’t just simply import the file into the database through a tool like PHPMyAdmin, as every row in the CSV should be modificated to fit with our new database design.

In this article, we will explain you our approach for reading efficiently a huge CSV file in PHP.

1. Split your file into smaller chunks

To get started, when we talk about huge files, we are not talking about files with 50K or 70K rows, we talk about millions of rows like in this example, with a CSV file of 25GB. So, the correct approach for such cases is not to work with the file directly, but with smaller files.

The smaller the file, the better will it be to have an optimal performance and control over your script, not only about the performance perspective but the logic as well. We wrote an article previously of how to split huge CSV datasets into smaller chunks using CSV splitter, a tool for Windows 10. You can of course do the same using another approach, but, you get the idea right? Split the file into smaller chunks that can be easily processed by your scripts later.

2. Implementing the read and iteration script

To read the file, we will use the fopen function of PHP, this inbuilt function is used to simply open a file from a local URL, it’s used to bind a resource to a steam. It expects as second argument the mode in which we’ll operate, in this case, just reading with the r identifier. The method returns a file pointer as long as the file exists, otherwise it will return False in case of failure.

The fgets function of PHP returns a line from an open file with fopen and it returns false when there’s nothing left to read. Then, inside the while loop, you will be able to parse the raw CSV string with the str_getcsv function. Having the basic stuff implemented, you will be ready to modify the script to do whatever you need to do with the best possible performance in PHP:

The advantages of this approach are:

Источник

How Can I Read a Large File Line by Line in PHP?

Author — Nitish Kumar

Every now and then, you will have to read and process a large file in PHP. If you are not careful when reading a large file, you will often end up exhausting all the memory of your server. However, there is no need to worry because PHP already provides a lot of inbuilt functions to read large files (by large I mean files with size over 1GB) either line by line or one chunk at a time. In this article, you will learn about the pros and cons of all these techniques.

Reading a File Line by Line into an Array Using file()

You can use the file() function in PHP to read an entire file into an array. This function stores each line along with the newline character in its own array element. If you intended to read a file line by line and store the result in an array, the file() function seems to a natural choice.

One major drawback of this method is that it will read the whole file at once. In other words, it solves the problem of reading a file one line at a time but it still reads the whole file at once. This means that you won’t be able to use it to read very large files.

In the example above, I have read a text file called The Life of the Bee from Project Gutenberg. This file is just 360kb long so reading it using the file() function was not a problem. Now we will learn how to read a file that might be over 1GB in size without exhausting our memory.

Reading a Large File One Line at a Time Using fgets()

If you have not specified a particular length that this function should read, it will read the whole line. In other words, the maximum memory that you need with fgets() depends on the longest line in the file. Unless the file that you are reading has very long lines, you won’t exhaust your memory like you could with the file() function in previous section.

Inside the if block, we read the first line of our file and if it is not strictly equal to false we enter the while loop and process the whole file one line at a time. We could also combine the file reading and checking process in one line like I have done the following example.

Reading a Large File in Smaller Pieces Using fread()

One drawback of reading files using fgets() is that you will need substantial amount of memory to read files which have very long lines. The situation is very unlikely to happen but if you can’t make any assumptions about the file it is better to read it in small sized chunks.

This is where the fread() function in PHP comes to our rescue. This function reads a file up to length number of bytes. Youare the one who gets to specify the length so you don’t have to worry about running out of memory.

In the above example, we read the large text file (1024*1024) 1MB at a time and it took 13 iterations to read the whole file. You can set the value of chunk_size to a reasonable value and read a very large file without exhausting your memory. You could use this technique to read a 1GB file using just 1MB of memory in about 1024 iterations. You can also read even larger files and increase the chunk_size if you want to keep the number of iterations low.

Quick Summary

Let’s recap everything that we have covered in this tutorial.

If you want to read files line by line into an array, using the file() function would be the right choice. The only drawback if that this function will read whole file at once so you won’t be able to read very large files without exhausting the memory.

If the files you are reading are very large and you have no plans to store the individual lines in an array, using the fgets() function would be the right choice. The amount of memory that your script needs will only depend on the length of largest line in the file that you are reading. This means that the file could be over 1GB in size but if the individual lines are not very large, you will never exhaust your memory.

If you are reading large files and have no idea how long individual lines could be in that particular file, using fread() would be your best bet. This function lets you specify the size of chunks in which you want to read the file. Another advantage of fread() over fgets() is that you will not have to perform a lot of iterations if the file you are reading contains large number of empty lines.

Let me know if there is anything that you would like me to clarify. Also, you are more than welcome to comment if you know other techniques for reading large files one line at a time in PHP.

Источник

Read a plain text file with php

I have a text file with this information in my server:

How do I read all the information from the text file (line by line) using PHP?

11 Answers 11

This will give you a line by line read.. read the notes at php.net/fgets regarding the end of line issues with Macs.

$array = explode(«\n», file_get_contents($filename));

This wont actually read it line by line, but it will get you an array which can be used line by line. There are a number of alternatives.

You can also produce array by using file:

php read file line by line. Смотреть фото php read file line by line. Смотреть картинку php read file line by line. Картинка про php read file line by line. Фото php read file line by line

php read file line by line. Смотреть фото php read file line by line. Смотреть картинку php read file line by line. Картинка про php read file line by line. Фото php read file line by line

And here: http://php.net/manual/en/function.fopen.php has more info on fopen including what the modes are.

What W3Schools says:

fopen opens the file (in this case test.txt with mode ‘r’ which means read-only and places the pointer at the beginning of the file)

The while loop tests to check if it’s at the end of file (feof) and while it isn’t it calls fgets which gets the current line where the pointer is.

Continues doing this until it is the end of file, and then closes the file.

Try something like this:

php read file line by line. Смотреть фото php read file line by line. Смотреть картинку php read file line by line. Картинка про php read file line by line. Фото php read file line by line

php read file line by line. Смотреть фото php read file line by line. Смотреть картинку php read file line by line. Картинка про php read file line by line. Фото php read file line by line

You can read a group of txt files in a folder and echo the contents like this.

php read file line by line. Смотреть фото php read file line by line. Смотреть картинку php read file line by line. Картинка про php read file line by line. Фото php read file line by line

php read file line by line. Смотреть фото php read file line by line. Смотреть картинку php read file line by line. Картинка про php read file line by line. Фото php read file line by line

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *