简体   繁体   中英

How can I tell what line a file resource is currently “on” in PHP?

Using PHP, it's possible to read off the contents of a file using fopen and fgets . Each time fgets is called, it returns the next line in the file.

How does fgets know what line to read? In other words, how does it know that it last read line 5, so it should return the contents of line 6 this time? Is there a way for me to access that line-number data?

(I know it's possible to do something similar by reading the entire contents of the file into an array with file , but I'd like to accomplish this with fopen .)

There is a "position" kept in memory for each file that is opened ; it is automatically updated each time you are reading a line/character/whatever from the file.

You can get this position with ftell , and modify it with fseek :

ftell — Returns the current position of the file read/write pointer

fseek — Seeks on a file pointer

You can also use rewind to... rewind... the position of that pointer.


This is not getting you a position as a line number, but closer to a position as a character number (actually, you are getting the position as a number of bytes from the beginning of the file) ; when you have that, reading a line is just a metter of reading characters until yu hit an end of line character.


BTW : as far as I remember, these functions are coming from the C language -- PHP itself being written in C ;-)

Files are just a stream of data, read from the beginning to the end. The OS will remember the position you've read so far in that file. If needed, doing so in the application as well is fairly simple. The OS only cares about byte positions though, not lines.

Just imagine dealing out a deck of 52 card sequentially. You hand off the first card. Next time the 2. card. When you want to give out the 3. card , you don't need to start counting from the start again, or even remembering where you were you just hand out the next available card, and that'll be the third.

It might be a bit more work that's needed to read lines, since you'd want to buffer data read from the actual file for preformance sake, but it's not that much more to it than to record the offset of the last piece of data you handed out, find the next newline character and hand off all the data between those 2 points.

PHP nor the OS has no real need to keep the line number around, since all the system care about is "next line". If you want to know the line number, you keep a counter and increment it every time your app reads a line.

$lineno=0;
while (!feof($handle)) {
    $buffer = fgets($handle, 4096);
    lineno++; // keep track of the line number
     ...

}

You could just call fgets and increment a var $line_number each time you call it. That would tell you the line it is on.

i hav this old sample i hob its can help you :)

$File = file('path');
$array = array();

$linenr = 5;

foreach( $File AS $line_num => $line )
{
$array = array_push( $array , $line );
}

echo $array[($linenr-1)];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM