Re: Saving an array as a file
| From: | Joe Stump | Date: | Thu, 02 Nov 2000 18:22:36 +0000 |
| Subject: | Re: Saving an array as a file | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-23435@lists.php.net to get a copy of this message | ||
> First, let me say your patience is astounding.
No biggie - I remember when I was a newbie and people would say "RTFM!" and it
would piss me off. I vowed then that if I ever got to the point where I was
releasing my own software and participating in the list I would NEVER do that
do a newbie - it doesn't foster growth of the community.
In order to write a log file you might want to check this page out:
http://www.php.net/manual/function.syslog.php
Otherwise you definately will want to use a+ to append to a file - if that does
not work try 'a+' instead of "a+" and then try it without the + (sometimes the
plus gives me problems...)
Take care,
--Joe
ps I cc'd this to the list for all the people out there that say RTFM!
>
> Okay, I'm really close on this. I can feel it.
>
> When I fopen() the file with 'w+', the file gets zeroed out when I try to
> write to it. All contents are deleted. When I use 'a' or 'a+', there is
> no
> change to the file at all. I'll provide all the code for you to peruse.
>
> Here we go:
>
> //Set log - set form value to Finance Form by cookie userid, if cookie
> exists
> if (isset($userid_cookie)) {
>
> //Open file, search for line with corresponding ID, change Form var to
> Finance Form, close file
> if (!$file=fopen("logfile/log.csv", "w+")) {
> echo("Could not open log file.");
> } else {
> $contents = fread($file,filesize("logfile/log.csv"));
> $array_of_lines = explode("\n",$contents);
> $line_count = count($array_of_lines);
> $incr = 1;
> While ($incr <= $line_count) {
> if (ereg("^$userid_cookie",$array_of_lines[$incr])) {
> //Modify line, replacing noform with Finance Form
> $array_of_lines[$incr] = ereg_replace("Finance
> Form","noform",$array_of_lines[$incr]);
> //Write changed line to file
> while (list($key,$val) = each($array_of_lines)){
> $line_to_write = $val . "\n";
> fwrite($file,$line_to_write);
> }
> //Break out of while loop
> break;
> }
> ++$incr;
> }
> fclose($file);
> }
> }
>
>
> -Jeff
>
> -----Original Message-----
> From: Joe Stump [mailto:joe@earth.care2.com]
> Sent: Thursday, November 02, 2000 10:43 AM
> To: Jeff Demel
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] Saving an array as a file
>
>
> > Sorry to be such a pain, but this is all new stuff to me.
>
> No worries - I'm here to help!
>
> <?
>
> // this will OVERWRITE the file each time
> // to APPEND to the file use a+
> $fp = fopen('/path/to/write/file','w+');
> while(list($key,$val) = each($array_of_lines)){
> $line_to_write = $val."\n";
> fwrite($fp,$line_to_write);
> }
> fclose($fp);
>
>
> ?>
>
>
> --Joe
================================================================================
= Joe Stump joestump98@yahoo.com http://www.miester.org =
================================================================================
"Real programmers don't comment their code.
It was hard to write, it should be hard to understand."