Reading specific line of a file in PHP

前端 未结 6 1565
攒了一身酷
攒了一身酷 2020-12-11 05:33

I am working on reading a file in php. I need to read specific lines of the file.

I used this code:

fseek($file_handle,$start);
while (!feof($file_ha         


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-11 05:54

    You could do like:

    $lines = file($filename); //file in to an array
    echo $lines[1];           //line 2
    

    OR

    $line = 0;
    $fh = fopen($myFile, 'r');
    
    while (($buffer = fgets($fh)) !== FALSE) {
       if ($line == 1) {
           // $buffer is the second line.
           break;
       }   
       $line++;
    }
    

提交回复
热议问题