-2

I am saving my data in text file line by line through PHP code and which is done successfully.

But when I am retrieving the data from text file I am getting the whole content which I have entered in a file which means whenever I enter a new line in the text file and reads that file I get whole content of the file. So my query is that how it is possible to read only new line when I read the text file.

This what I have tried

$file = $_POST['read_fileid'];
$myFile = $file.".txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
varun
  • 472
  • 4
  • 8
  • 24

1 Answers1

1

fgets() function can be used to read the file line by line:

$handle = fopen("input.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false)
    {
        //code to read file`       
    }
}
else
{
    // error : file cannot be opened
}

fclose($handle);
Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
Naive
  • 492
  • 3
  • 8
  • 20