I have the following script that is supposed to loop through the contents of a file and remove all the empty lines then rewrite the file only without the empty lines. Without writing to the file, or in otherwords by just having it echo the line, it works flawlessly. However when I try to get it to write to a file it just erases all the content of the file. No errors are returned. I'm open to new ways of the script running so anyway to achieve what I'm looking for would be great. Thanks in advance.
error_reporting(E_ALL);
ini_set('display_errors', true);
$filenameindex = "somefile";
$filecont = file($filenameindex);
$fileindex = fopen($filenameindex, "w") or die("can't open file");
foreach($filecont as $line)
{
if(strlen($line) > 0)
{
fwrite($fileindex,$line);
}
}
fclose($fileindex);