ajd344 Posted August 19, 2007 Share Posted August 19, 2007 Hey, I'm appending information to a regular text file (not html). I need to put a new line in for each entry added on, not just add on to it. I tried \n, but it didnt work, it just made a weird square at the end. Any ideas? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/65650-append-to-new-line-in-text-file/ Share on other sites More sharing options...
hostfreak Posted August 19, 2007 Share Posted August 19, 2007 Have you looked at fwrite() in the manual (http://www.php.net/manual/en/function.fwrite.php)? \n should work... did you enclose it in single quotes or double? It would only parse in double. Edit: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single to further explain what I mean. Quote Link to comment https://forums.phpfreaks.com/topic/65650-append-to-new-line-in-text-file/#findComment-327897 Share on other sites More sharing options...
ajd344 Posted August 19, 2007 Author Share Posted August 19, 2007 Well, I'm trying to figure this out, but I cant. Here, maybe you spot something in my code: $myFile = "data/data.txt"; $fh = fopen($myFile, 'a') or die("error"); fwrite($fh, "$out"); fclose($fh); The output is like this: ([] being a weird square) entryone[]entrytwo When I copy and paste that square, it makes a new line. Huh. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/65650-append-to-new-line-in-text-file/#findComment-327900 Share on other sites More sharing options...
hostfreak Posted August 19, 2007 Share Posted August 19, 2007 Where is $out defined? Quote Link to comment https://forums.phpfreaks.com/topic/65650-append-to-new-line-in-text-file/#findComment-327903 Share on other sites More sharing options...
ajd344 Posted August 19, 2007 Author Share Posted August 19, 2007 Uhm, right above it. The code snip is just the part that works with the file Quote Link to comment https://forums.phpfreaks.com/topic/65650-append-to-new-line-in-text-file/#findComment-327907 Share on other sites More sharing options...
hostfreak Posted August 19, 2007 Share Posted August 19, 2007 From the manual, with little modification to fit your variable names etc: <?php $myFile = 'data/data.txt'; $out = "Add this to the file\n"; $handle = fopen($myFile, 'a'); // Let's make sure the file exists and is writable first. if (is_writable($myFile)) { // In our example we're opening $filename in append mode. // The file pointer is at the bottom of the file hence // that's where $out will go when we fwrite() it. if (!$handle = fopen($myFile, 'a')) { echo "Cannot open file ($myFile)"; exit; } // Write $out to our opened file. if (fwrite($handle, $out) === FALSE) { echo "Cannot write to file ($myFile)"; exit; } echo "Success, wrote ($out) to file ($myFile)"; fclose($handle); } else { echo "The file $myFile is not writable"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65650-append-to-new-line-in-text-file/#findComment-327908 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.