1

I have a code

$handle = fopen(getcwd() . "/emails.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        SendEmails($line,$strSubject,$strMessage,$txtFormName,$txtFormEmail,$fname,$ftype,$tmp_path);
        $line.delete; // here i need to delete each readed line
    }
    fclose($handle);
} else {
    echo "error opening the file.";
} 

How to delete each readed line while reading?

Dmitrij Holkin
  • 1,995
  • 3
  • 39
  • 86
  • this might help you http://stackoverflow.com/questions/5712878/how-to-delete-a-line-from-the-file-with-php – urfusion Jan 07 '16 at 08:21
  • Because something wrong with execution and fopen start to duplicate lines, i think this hapens when browser timeout is. So if there was empty string fopen read them but i need only filed strings – Dmitrij Holkin Jan 07 '16 at 08:26

2 Answers2

1

Try This

$handle = fopen(getcwd() . "/emails.txt", "r");
$readedlines="";
if ($handle) {
while (($line = fgets($handle)) !== false) {
SendEmails($line,$strSubject,$strMessage,$txtFormName,$txtFormEmail,$fname,$ftype,$tmp_path);
   $readedlines=$readedlines.$line;
fclose($handle);
$newFileContent=str_replace($readedlines,"",file_get_contents($filename));
$handle=fopen($filename, "w");
fwrite($handle,$newFileContent);
fclose($handle);
} else {
echo "error opening the file.";
 } 

I mean just concate all readed line into one string and replace through the whole string of file content. Hope that helps

Nisarg Desai
  • 361
  • 3
  • 16
1
// Get file contents
$contents = file_get_contents('/emails.txt');

// Explode to get contents in an array
$rows = explode("\n", $contents);

file_put_contents('/emails.txt', "");

// Loop through all rows in emails.txt
foreach ($rows as $row) {
    // If sending the email fails, add row to $notSent
    if (false === SendEmails($row, $strSubject, $strMessage, $txtFormName, $txtFormEmail, $fname, $ftype,$tmp_path)) {
        $contents = file_get_contents('/emails.txt');
        $contents .= $row."\n";
        file_put_contents('/emails.txt', $contents);
    }
}
Peter
  • 8,776
  • 6
  • 62
  • 95