Page 1 of 1

Saving to a text file an assigned value

Posted: Mon Mar 15, 2004 7:47 am
by rob64464
Hello,
I am a noob to PHP and I was wondering. How do I save text from the
url to a text file? For example

http://www.mydomain.com/store.php?value=12334567

how do I save value to a text file?

Thanks a lot,
Robert

Re: Saving to a text file an assigned value

Posted: Mon Mar 15, 2004 8:38 am
by TheBentinel.com
rob64464 wrote:Hello,
I am a noob to PHP and I was wondering. How do I save text from the
url to a text file? For example

http://www.mydomain.com/store.php?value=12334567

how do I save value to a text file?

Thanks a lot,
Robert
You can use file_get_contents() to get the text into a variable, then use file_put_contents() to put it into a file.

http://www.php.net/file_get_contents
http://www.php.net/file_put_contents

If your version of PHP doesn't support one or both of those, the manual pages give you alternatives.

Posted: Mon Mar 15, 2004 4:16 pm
by rob64464
Have you got any code? I can't seem to get it working at all! Any code would be a great help!

Thanks,
Rob

Posted: Mon Mar 15, 2004 5:12 pm
by Illusionist
well what have you tried??

To get teh value of value use

Code: Select all

$value = $_GET['value'];

Posted: Mon Mar 15, 2004 5:12 pm
by m3mn0n
[php_man]file_get_contents[/php_man]() is easy to use, and is availible for use in the latest stable release of 4.x.

However, [php_man]file_put_contents[/php_man]() is only found in version 5.x so you'll need to use [php_man]fopen[/php_man]()/[php_man]fwrite[/php_man]()/[php_man]fclose[/php_man]() to complete the task.

Goto the fopen manual page, there is some more details about writing to files and also demostration code. Don't forget to read the user notes too.

Posted: Mon Mar 15, 2004 5:55 pm
by tim

Code: Select all

<?php
$log = "data.txt";
if(file_exists("$log")) {
 $write = fopen($log, "r+");
 fwrite($write, $value);
 fclose($write);
} else {
echo "$log could not be found";
}
?>
not tested, but should work.

uses fopen, n fwrite to get the txt and write (as suggested)

Posted: Tue Mar 16, 2004 8:54 am
by rob64464
thanks you guys got the code working! :D:D:D

thanks,
Rob