Jump to content

Recommended Posts

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?

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);

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'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?

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

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.

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 ;)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.