Page 1 of 1

Writing to files on multiple lines! Need help

Posted: Wed Mar 24, 2004 12:52 pm
by andylyon87
How do I go about writing to one line of a file for one piece of information and then the next piece of information is on the next line. Can you make it so the info gets written at the same time or do you have to use multiple fwrite functions.
My current file writing situation is as follows:

Code: Select all

function WriteToFile($firstname){
         $TheFile = "php/form_data/".time().".txt";
         $Open = fopen($TheFile,"a");
          if($Open){
                fwrite($Open, "$firstname\n");
                fclose($Open);
                   $Worked=True;
         }else{
           $Worked = False;
          }
          return $Worked;
          }
Thats the function and heres the call function:

Code: Select all

$callfunction = WriteToFile("$arrayїname],$arrayїscreen_name],$arrayїreview_comp],$arrayїreview]\n");
I have also tried using \n after each piece of info instead of a comma

Posted: Wed Mar 24, 2004 12:53 pm
by malcolmboston
for windows the line ending (\n)
is
\r\n

try that

Posted: Wed Mar 24, 2004 12:57 pm
by andylyon87
so like this:
$callfunction = WriteToFile("$array[name]\r\n$array[screen_name]\r\n$array[review_comp]\r\n$array[review]\n");

Posted: Wed Mar 24, 2004 1:04 pm
by malcolmboston
looks good, try it out

im on not on my web server so can't

Posted: Wed Mar 24, 2004 1:06 pm
by andylyon87
jus tried it doesn't seem to be working. Would I need more arguements in the WriteToFile function, at present their is only $firstname

Posted: Wed Mar 24, 2004 1:08 pm
by malcolmboston
one sec ill go on my web server and download a file i made, it does exactly what your asking, maybe you can examine it and adapt it for your needs

Posted: Wed Mar 24, 2004 1:11 pm
by malcolmboston
here ya go

Code: Select all

<?php
$filename = 'admin_access.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // The file pointer is at the bottom of the file hence 
    // that's where $somecontent will go when i fwrite() it.
    if (!$handle = fopen($filename, 'a+')) {
         print "This area is currently not being monitored";
         exit;
    }

    // Write $somecontent to our opened file.
    if (!fwrite($handle,"$time $date --- $_SESSION[username] Was viewing the admin login screen\n")) {
        print "Cannot write to file ($filename)";
        exit;
    }
    
    print "Your actions are being recorded, this is for security";
    
    fclose($handle);
					
} else {
    print "The file $filename is not writable";
}
?>
this should suit you perfectly

Posted: Wed Mar 24, 2004 1:15 pm
by malcolmboston
just so you know this is what it outputs for me (notice new line) works on both linux and windows

3:47pm 28 January 2004 --- malcolmboston Was viewing the admin login screen
12:24pm 29 January 2004 --- malcolmboston Was viewing the admin login screen
5:15pm 29 January 2004 --- malcolmboston Was viewing the admin login screen
5:16pm 29 January 2004 --- malcolmboston Was viewing the admin login screen
2:20pm 30 January 2004 --- malcolmboston Was viewing the admin login screen
2:20pm 30 January 2004 --- malcolmboston Was viewing the admin login screen
4:40pm 2 February 2004 --- malcolmboston Was viewing the admin login screen
3:10pm 3 February 2004 --- malcolmboston Was viewing the admin login screen
12:18pm 4 February 2004 --- malcolmboston Was viewing the admin login screen
12:18pm 4 February 2004 --- malcolmboston Was viewing the admin login screen
12:21pm 4 February 2004 --- malcolmboston Was viewing the admin login screen
2:58pm 6 February 2004 --- malcolmboston Was viewing the admin login screen
2:59pm 6 February 2004 --- malcolmboston Was viewing the admin login screen
10:32am 9 February 2004 --- malcolmboston Was viewing the admin login screen
10:52am 9 February 2004 --- malcolmboston Was viewing the admin login screen
11:21am 15 February 2004 --- malcolmboston Was viewing the admin login screen
6:35pm 18 February 2004 --- malcolmboston Was viewing the admin login screen

Posted: Wed Mar 24, 2004 1:26 pm
by andylyon87
thats not quite what I mean, the file it writes to is time().txt so it is always different. So how could I say in the first example above, get the 3:47pm to be on the first line and then on the next line have the 28 Jan 2004 then the next line malcomboston etc.

Posted: Wed Mar 24, 2004 1:28 pm
by malcolmboston
???

Code: Select all

(!fwrite($handle,"$time\n$date\n$_SESSION[username]\n"))
im sorry i havent done anything with files in months

always if \n doesnt work try \r\n

Posted: Wed Mar 24, 2004 1:41 pm
by andylyon87
thanks for help but doesn't seem to be workin

Posted: Wed Mar 24, 2004 1:48 pm
by malcolmboston
from PHP manual wrote: When you try to write some newline \n or \r\n in Windows style, you have to write your content as :

$some_content = "blabla \n blabah \n";

and not as :

$some_content = 'blabla \n blabah \n';

With single quote ', it doesn't work, but it does with double.
]
fromt php manual wrote: If you are writing data to a txt file on a windows system and need a line break. use \r\n . This will write hex OD OA.

i.e.
$batch_data= "some data... \r\n";
fwrite($fbatch,$batch_data);

The is the equivalent of opening a txt file in notepad pressing enter and the end of the line and saving it.
i know i keep saying this but seriously, i love the manual more than i love my own mother, so should you too
more info here

Posted: Wed Mar 24, 2004 2:30 pm
by redmonkey
andylyon87 wrote:so like this:
$callfunction = WriteToFile("$array[name]\r\n$array[screen_name]\r\n$array[review_comp]\r\n$array[review]\n");
Try enclosing your variables in curly brackets like this.....

Code: Select all

WriteToFile("{$array[name]}\n{$array[screen_name]}\n{$array[review_comp]}\n{$array[review]}");
Most text editors these days recognise either \r\n or \n as a new line however, if you still don't see new lines then try replacing the \n above with \r\n you don't require the \n at the end of the call as this is handled by the function.