NEW: function add_text_to_file($file, $text) { if(file_exists($file) and is_writable($file)) { $filecontents = file($file); $fout = fopen($file, "w"); $filecontents = array_map('rtrim', $filecontents); array_push($filecontents, $text); $filecontents = array_unique($filecontents); $file_text = implode("\n", $filecontents); fwrite($fout, $file_text); fclose($fout); return true; } else { return false; } } OLD: function add_text_to_file($file, $text) { if(file_exists($file) and is_writable($file)) { $filecontents = split("\n", file_get_contents($file)); $fout = fopen($file, "w"); $new_file_text = ""; foreach($filecontents as $line) { if($line) $new_file_text .= rtrim($line) . "\n"; } $new_file_text .= $text . "\n"; $file_text = str_replace("\n\n", "\n", $new_file_text); fwrite($fout, $file_text); fclose($fout); return true; } else { return false; } }