PDA

View Full Version : Writing to a text file


Charl
01-13-2003, 12:35 AM
Can someone tell me what’s wrong with these code snippets?
I’ve chmod the text file to 755, 666, and 777, pretty much everything and got it to work once but now it doesn’t.

if(isset($_POST["submit"]))
{

$REMOTE_ADDR=$_POST["REMOTE_ADDR"];
$REMOTE_PORT=$_POST["REMOTE_PORT"];
$host=$_POST["host"];
$HTTP_VIA=$_POST["HTTP_VIA"];

$fp=fopen("User.txt","a");

if(!$fp)
{
print("Can't open your file'");
exit;

}
fwrite($fp," **************************************************

*****\r\n\r\n");
fwrite($fp,"Name:\t\t\t$REMOTE_ADDR\r\n");
fwrite($fp,"E-mail:\t\t$REMOTE_PORT\r\n\r\n");
fwrite($fp,"Password:\t\t$host\r\n\r\n");
fwrite($fp,"Password:\t\t$HTTP_VIA\r\n\r\n");

fclose($fp);

}

or

$fp = fopen("User.txt", "wb") or die ("The File \"User.txt\" does not exist");
flock( $fp, 2);
fputs ($fp, "<?php\n\$REMOTE_ADDR = \"$REMOTE_ADDR\";\n\$REMOTE_PORT = \"$REMOTE_PORT\";\n\$REMOTE_USER = \"$REMOTE_USER\";\n\$HTTP_VIA = \"$HTTP_VIA\";\n\$host = \"$host\";\n?>");
flock( $fp, 1);
fclose ($fp);
include("User.txt");

firepages
01-13-2003, 03:02 AM
.... what is not happening ? or what errors are you getting ? - is the file being writen at all or written and empty ?

need more info !

Charl
01-13-2003, 03:54 AM
It’s not writing any text to the User.txt file.

I got the second script to work 1 time, but then nothing. I have them all in the same directory and even put a full path for the text file.

It’s not giving me any kind of error, it all loads fine on a page by itself and a page with other php/html on.

- is the file being written at all or written and empty? How do I tell if that’s happening?

duniyadnd
01-13-2003, 05:24 PM
Couple of things you might want to consider.

If you got the same variables coming in, its probably just replacing the old ones, with the news ones, so you won't see a change, because the pointer starts at 0. If you want to add an entry to the end of the page, you do this:

while (!feof($fp))
{
$buffer = fgets($fp, 4096);
}

fputs($fp, $string);
.
.

Let me explain what this does: feof function looks for the end-of-file pointer, so while we not at the end of the file, we keep incrementing it in $fp to reach the end, so we know we're at the end of the page.
fputs() -> this function puts the information at where the pointer is located at that point of time, replacing anything that's in the way, but since you at the end of the file, you should be adding on information instead of replacing.

Replace $string with whatever you had in there as the attribute.

Second thing you might want to consider is the possibility of the text file being updated, but not the output on the browser (at times, the browser is one step behind, if you echo what's in the file before you make the changes esp., its a common programming mistake)

Hope that helped.
Duniyadnd

Charl
01-14-2003, 01:37 AM
Thanks for the reply duniyadnd but i still cant get it to work.

Heres whats happening

I tried this simple script just to see what happens. When i run the page the User.txt doesnt change at all. Then when i CHMOD the User.txt to 767 it updates itself and logs the php script. Run the script again and nothing happens but CHMOD the User.txt back to 777 and it logs the information again. Does anyone know whats going on with it.

<?php
$fo = fopen("User.txt","a");
fputs($fo,"$REMOTE_ADDR\n");
fclose($fo);
?>

weird eh

missing-score
02-12-2003, 10:13 PM
Not sure, but it is fopen($file,'a+');

Just a guess... I seem to remember reading that on www.php.net