Php read specific line from file
I'm trying to read a specific line from a text file using php. Here's the text file: Show
How would I get the content of the second line using php? This returns the first line:
..but I need the second. Any help would be greatly appreciated
asked Apr 25, 2011 at 5:22
file — Reads entire file into an array
Blender 280k52 gold badges426 silver badges487 bronze badges answered Apr 25, 2011 at 5:25 7 omg I'm lacking 7 rep to make comments. This is @Raptor's & @Tomm's comment, since this question still shows up way high in google serps. He's exactly right. For small files I just ran a tiny test with a *.csv with a file size of ~67mb (1,000,000 lines):
And
since noone mentioned it yet, I gave the
This was on my Win7 desktop so it's not representative for production environment, but still ... quite the difference. answered Jul 5, 2015 at 21:46
nimmneunnimmneun 1,10914 silver badges16 bronze badges 2 If you wanted to do it that way...
Alternatively, open it with
answered Apr 25, 2011 at 5:25
alexalex 467k197 gold badges865 silver badges975 bronze badges 2 I would use the SplFileObject class...
answered Feb 25, 2016 at 21:15
PhilPhil 3,9498 gold badges58 silver badges103 bronze badges 1 you can use the following to get all the lines in the file
and
Snake Eyes 15.8k32 gold badges107 silver badges208 bronze badges answered Apr 25, 2011 at 5:32
balanvbalanv 10.5k27 gold badges90 silver badges137 bronze badges 1
answered Apr 25, 2011 at 5:27
PhoenixPhoenix 4,4181 gold badge19 silver badges13 bronze badges 2 This question is quite old by now, but for anyone dealing with very large files, here is a solution that does not involve reading every preceding line. This was also the only solution that worked in my case for a file with ~160 million lines.
It works by opening the file without reading anything, then moving the pointer instantly to a random position, reading up to 4096 characters from that point, then grabbing the first complete line from that data. answered Dec 19, 2016 at 2:54
cantelopecantelope 1,1277 silver badges9 bronze badges If you use PHP on Linux, you may try the following to read text for example between 74th and 159th lines:
This solution is good if your file is large. answered Oct 12, 2015 at 11:42
minerootmineroot 1,58715 silver badges23 bronze badges 1 You have to loop the file till end of file.
answered Apr 25, 2011 at 5:25
RikeshRikesh 25.8k14 gold badges77 silver badges86 bronze badges 0 You could try looping until the line you want, not the EOF, and resetting the variable to the line each time (not adding to it). In your case, the 2nd line is the EOF. (A for loop is probably more appropriate in my code below). This way the entire file is not in the memory; the drawback is it takes time to go through the file up to the point you want.
answered Nov 3, 2015 at 1:07
I like daggett answer but there is another solution you can get try if your file is not big enough.
answered Nov 28, 2015 at 15:17
drugandrugan 7208 silver badges10 bronze badges I searched for a one line solution to read specific line from a file. Here my solution:
answered Jan 27, 2021 at 17:44
How read a specific line from a file in PHP?php $myFile = "4-24-11. txt"; $fh = fopen($myFile, 'r'); $theData = fgets($fh); fclose($fh); echo $theData; ?> ... Note: that should be $lines[1] ... . Thanks man! ... . if the file size is huge, this solution will be slow and occupying a lot memory. ... . Still, 40+ upvotes?. Which of the following is the correct way to open read text file in PHP?PHP Open File - fopen()
A better method to open files is with the fopen() function. This function gives you more options than the readfile() function.
|