Writing to file with function

From: Date: Sat, 12 Jul 2003 21:03:02 +0000
Subject: Writing to file with function
Groups: php.general 
Request: Send a blank email to php-general+get-154973@lists.php.net to get a copy of this message
I have written this function to rewrite the contents of a text file after it's been updated. I store lines of data in an array and then implode them into one string. The problem is that I'm getting an extra line break (in other words a blank line is showing up in my text file) after I modify a particular line. I checked the imploded string with var_dump and don't see any line breaks besides the ones that separate each line. Anyone have any ideas as to why I'm getting an extra line break? The function is below with the code that calls it below that. Jason // Rewrite data file from array of file lines function rewrite_file($lines) { $filecontents = implode("\n", $lines); $fh = fopen("data.txt", "w") or die("Could not open file."); flock($fh, LOCK_EX); if (fwrite($fh, $filecontents) == -1) { die("File could not be written to."); } ftruncate($fh, ftell($fh)) or die("Error."); flock($fh, LOCK_UN); fclose($fh) or die("Error."); } <-###########################################################-> $uid = $_POST['id']; $title = stripslashes(strip_tags(trim($_POST['title']))); $name = array_search($username, $full_name); $email = array_search($username, $user_email); $date = $_POST['date']; $category = $_POST['category']; $article = stripslashes(trim($_POST['article'])); $data_array = array($uid, $title, $name, $email, $date, $category, $article); $data_line = implode($separator, $data_array)."\n"; $filelines = return_lines(); // Reads file and returns array of it's lines. for ($i = 0; $i <= count($filelines); $i++) { // loop through array in order if (strstr($filelines[$i], $uid) != FALSE) { $data_pos = array_search($filelines[$i], $filelines); array_splice($filelines, $data_pos, 1, $data_line); } } rewrite_file($filelines);

« previous php.general (#154973) next »