bloodgoat Posted March 30, 2009 Share Posted March 30, 2009 Say this is my file: file.php <?php $array = array(); $array[] = array("val 1-1", "val 1-2", "val 1-3"); $array[] = array("val 2-1", "val 2-2", "val 2-3"); $array[] = array("val 3-1", "val 3-2", "val 3-3"); ?> Now, I want to fwrite another string to my array $array[] = array("val 4-1", "val 4-2", "val 4-3"); How do I append this to the end (preceeding the PHP closing tag, obviously) without rewriting the entire file/every value in the file? Quote Link to comment https://forums.phpfreaks.com/topic/151842-solved-writing-to-a-file-without-rewriting-the-entire-file/ Share on other sites More sharing options...
MadTechie Posted March 30, 2009 Share Posted March 30, 2009 use fopen in append mode (a) ie <?php /*Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. */ $fp = fopen("c:\\data\\info.txt", "a"); //a = append mode fwrite($fp, 'more stuff to add to the end'); fclose($fp); Quote Link to comment https://forums.phpfreaks.com/topic/151842-solved-writing-to-a-file-without-rewriting-the-entire-file/#findComment-797330 Share on other sites More sharing options...
bloodgoat Posted March 30, 2009 Author Share Posted March 30, 2009 I was wondering about that, but it says "end of the file" so I assume that it means even after the closing PHP tag. Is this the case? Quote Link to comment https://forums.phpfreaks.com/topic/151842-solved-writing-to-a-file-without-rewriting-the-entire-file/#findComment-797335 Share on other sites More sharing options...
sdi126 Posted March 30, 2009 Share Posted March 30, 2009 I'm confused...your talking about arrays and also writing to files ??? But like MadTechie stated using the "a" option in the fopen function appends data...the "end of file" your asking about means add data to the end of the file your writing to...its not talking about adding code to your "file" ( the php page at the end) Quote Link to comment https://forums.phpfreaks.com/topic/151842-solved-writing-to-a-file-without-rewriting-the-entire-file/#findComment-797342 Share on other sites More sharing options...
bloodgoat Posted March 30, 2009 Author Share Posted March 30, 2009 I'm confused...your talking about arrays and also writing to files ??? But like MadTechie stated using the "a" option in the fopen function appends data...the "end of file" your asking about means add data to the end of the file your writing to...its not talking about adding code to your "file" ( the php page at the end) I just use all my values in arrays for ease of access and manipulation. What I'm writing is entirely irrelevant, I suppose, I'm just using arrays in my demonstration in case it makes any difference whatsoever. As I said, my original file would contain those first three arrays. Upon execution of another file, I want to append a new array to the end of the already existing arrays. (Although you could regard the arrays as anything at all, the arrays are just more true to what I'm trying to create.) My concern lies in the writing mode. As I read things VERY literally, when I see the description for writing mode "a", I assume that when the fwrite was executed, the file would go from: <?php $array = array(); $array[] = array("val 1-1", "val 1-2", "val 1-3"); $array[] = array("val 2-1", "val 2-2", "val 2-3"); $array[] = array("val 3-1", "val 3-2", "val 3-3"); ?> To: <?php $array = array(); $array[] = array("val 1-1", "val 1-2", "val 1-3"); $array[] = array("val 2-1", "val 2-2", "val 2-3"); $array[] = array("val 3-1", "val 3-2", "val 3-3"); ?> $array[] = array("val 4-1", "val 4-2", "val 4-3"); As in, the new string is applied to the end, the VERY end, of the file it is being written to. I suppose my question ought to be: is this the case? Quote Link to comment https://forums.phpfreaks.com/topic/151842-solved-writing-to-a-file-without-rewriting-the-entire-file/#findComment-797350 Share on other sites More sharing options...
MadTechie Posted March 30, 2009 Share Posted March 30, 2009 Okay i think i get what you mean.. your writing a PHP file.. well firstly.. thats mostly a bad idea.. but try this (very untested code) <?php /*Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. */ $fp = fopen("c:\\data\\info.txt", "r"); //a = append mode fseek($fp, -2, SEEK_END); //goback 2 from the end fwrite($fp, 'echo "the end"; ?>'); fclose($fp); ?> EDIT: Yep i was right. lol phpfreaks parse is confused with that code Quote Link to comment https://forums.phpfreaks.com/topic/151842-solved-writing-to-a-file-without-rewriting-the-entire-file/#findComment-797358 Share on other sites More sharing options...
bloodgoat Posted March 30, 2009 Author Share Posted March 30, 2009 Okay i think i get what you mean.. your writing a PHP file.. well firstly.. thats mostly a bad idea.. but try this (very untested code) <?php /*Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. */ $fp = fopen("c:\\data\\info.txt", "r"); //a = append mode fseek($fp, -2, SEEK_END); //goback 2 from the end fwrite($fp, 'echo "the end"; ?>'); fclose($fp); ?> EDIT: Yep i was right. lol phpfreaks parse is confused with that code Perhaps if I explain why I'm writing a PHP file, and if there's a better method (other than using databases), you can explain a better route? As a part of the CMS I'm developing, a segment of it will consist of a log that tracks users on the site. The date (val 1), the page viewed (val 2), their IP address (val 3), and the browser they use (val 4). Instead of writing this all to an HTML file, I'm using arrays and rsort()'ing them to view the newest at the top. In addition, I want the default value of array strings displayed to be 150 (run through a for() loop), but I also want the option for the admin to input a value of their choosing and view that many of the most recent log records. Ie/ ?view=(nothing) displays 150, ?view=100 displays 100, ?view=16 displays 16). For this reason, I believed it was most feasible, without using databases, to use arrays. Quote Link to comment https://forums.phpfreaks.com/topic/151842-solved-writing-to-a-file-without-rewriting-the-entire-file/#findComment-797368 Share on other sites More sharing options...
MadTechie Posted March 31, 2009 Share Posted March 31, 2009 IMHO, i would personally create a CSV file, in addion i would append the date to the filename (to allow easier management over what logs i wish to delete later or even archive, to display the file you can use function file (Reads entire file into an array) rsort it and then explode the lines (if needed) example (untested) <?php $display = 16; #$newlines = array(); // Get a file into an array $lines = file('mylog.csv'); rsort($lines); for($n=0,$n=<$display;$n++) { $line = $lines[$n]; #$newlines = explode(",",$line); //if you want an array echo "$line<br>"; //just echo the lines } ?> EDIT: oh the added bonus is you can download locally for archive and review in excel Quote Link to comment https://forums.phpfreaks.com/topic/151842-solved-writing-to-a-file-without-rewriting-the-entire-file/#findComment-797392 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.