Php read file line line




















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:. Also it is easy to introduce a counter if you want to limit the number of lines. The only cases you'd not use his answer that I know of is if file or like functions are disabled via php.

Note: Error handling not included. 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.

This code read file backwards. This code ignore modifications on reading, example apache access. This code is verbatim so you should famous last words be able to copy it into a file on your server and run it. Though you may well need to change the filename in the fopn call. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Read a file backwards line by line using fseek Ask Question. Asked 11 years, 6 months ago.

Active 3 months ago. Viewed 30k times. This was not the answer to the question asked, but it does answer the more common question many people have when looking here, so it was still useful, thanks. Especially when you want an array as the end result. This breaks badly on large files, so it is exactly the method that does not work. Use buffering techniques to read the file.

Starx Starx 74k 43 43 gold badges silver badges bronze badges. I wouldn't be surprised if the OP didn't really care about actual lines and just wanted to e. In that case, this answer is just fine and what most PHP coders would do anyway. Sir, How will you locate the file inside the fopen by the way?

Assuming we need to specify the url for opening! John John 6, 3 3 gold badges 47 47 silver badges 49 49 bronze badges.

This way I got the newline from the end of each line included. ValterEkholm Yes, the newline from the end of each line becomes another normal character, as the delimiter is not the newline anymore.

Quolonel Questions Quolonel Questions 5, 2 2 gold badges 29 29 silver badges 33 33 bronze badges. Memory efficient compared to file. This how I manage with very big file tested with up to G. Metodi Darzev Metodi Darzev 1 1 silver badge 4 4 bronze badges. OmarElDon what do you mean?

Buffering is usually faster so this is probably a better solution if you don't mind the added complexity. Cuse70 Cuse70 3 3 silver badges 5 5 bronze badges. SplFileObject is useful when it comes to dealing with large files. Tegan Snyder Tegan Snyder 7 7 silver badges 10 10 bronze badges. Using rtrim is shorter for the same purpose : And it is cross-platformed.

Yaroslav Shabalin 1, 3 3 gold badges 17 17 silver badges 29 29 bronze badges. This would create a single array of more than one GB in memory good luck with it divided not even in lines but in arbitrary character chunks. Why on earth would you want to do that? Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name.

Email Required, but never shown. It's a good programming practice to close all files after you have finished with them. You don't want an open file running around on your server taking up resources!

The fclose requires the name of the file or a variable that holds the filename we want to close:. Note: After a call to the fgets function, the file pointer has moved to the next line. The feof function checks if the "end-of-file" EOF has been reached. The feof function is useful for looping through data of unknown length. The example below reads the "webdictionary.

The fgetc function is used to read a single character from a file. Note: After a call to the fgetc function, the file pointer moves to the next character. Open a file, and write the correct syntax to output one character at the time, until end-of-file. We just launched W3Schools videos. Get certified by completing a course today!



0コメント

  • 1000 / 1000