Saving to a text file an assigned value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rob64464
Forum Newbie
Posts: 3
Joined: Mon Mar 15, 2004 7:47 am
Contact:

Saving to a text file an assigned value

Post 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
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: Saving to a text file an assigned value

Post 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.
rob64464
Forum Newbie
Posts: 3
Joined: Mon Mar 15, 2004 7:47 am
Contact:

Post 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
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

well what have you tried??

To get teh value of value use

Code: Select all

$value = $_GET['value'];
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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)
rob64464
Forum Newbie
Posts: 3
Joined: Mon Mar 15, 2004 7:47 am
Contact:

Post by rob64464 »

thanks you guys got the code working! :D:D:D

thanks,
Rob
Post Reply