By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,827 Members | 813 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,827 IT Pros & Developers. It's quick & easy.

Save PHP variables to a text file

P: n/a
I was wondering how to save PHP variables to a txt file and then
retrieve them again.

Example:

There is an input box, after submitted the stuff that was written in
the input box will be saved to a text file. Later on the results need
to be brought back as a variable. So lets say the variable is $text I
need that to be saved to a text file and be able to retrieve it back
again.
Hope it makes sense,

Thanks in advance!!!

Oct 1 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
On Sep 30, 7:30 pm, Lamer <Galat...@gmail.comwrote:
I was wondering how to save PHP variables to a txt file and then
retrieve them again.

Example:

There is an input box, after submitted the stuff that was written in
the input box will be saved to a text file. Later on the results need
to be brought back as a variable. So lets say the variable is $text I
need that to be saved to a text file and be able to retrieve it back
again.

Hope it makes sense,

Thanks in advance!!!
I believe serialize is usually used in cases like this:

http://www.php.net/manual/en/function.serialize.php

A while back, I wrote a function called array_smart_dump (yes, the
name makes me giggle, too) that accomplishes the same thing with
arrays a bit more literally.

It will convert an array to a string that, as I put it, is
syntactically usable, meaning you can write the string (enclosed by
appropriate php tags) to file and then later use that array by simply
including the file.

The code can be found here:

http://klenwell.googlecode.com/svn/t...t_dump.inc.php

Here's a test script:

http://klenwell.googlecode.com/svn/t...smart_dump.php

Tom

Oct 1 '07 #2

P: n/a
On Sep 30, 8:18 pm, klenwell <klenw...@gmail.comwrote:
On Sep 30, 7:30 pm, Lamer <Galat...@gmail.comwrote:
I was wondering how to save PHP variables to a txt file and then
retrieve them again.
Example:
There is an input box, after submitted the stuff that was written in
the input box will be saved to a text file. Later on the results need
to be brought back as a variable. So lets say the variable is $text I
need that to be saved to a text file and be able to retrieve it back
again.
Hope it makes sense,
Thanks in advance!!!

I believe serialize is usually used in cases like this:

http://www.php.net/manual/en/function.serialize.php

A while back, I wrote a function called array_smart_dump (yes, the
name makes me giggle, too) that accomplishes the same thing with
arrays a bit more literally.

It will convert an array to a string that, as I put it, is
syntactically usable, meaning you can write the string (enclosed by
appropriate php tags) to file and then later use that array by simply
including the file.

The code can be found here:

http://klenwell.googlecode.com/svn/t...y_smart_dump.i...

Here's a test script:

http://klenwell.googlecode.com/svn/t...s/test.array_s...

Tom
What if I want to simply save one result to a text file and be able to
retrieve it as a variable?

Oct 1 '07 #3

P: n/a
On Sep 30, 8:31 pm, Bryan <Bryan.Andr...@gmail.comwrote:
On Sep 30, 8:18 pm, klenwell <klenw...@gmail.comwrote:
On Sep 30, 7:30 pm, Lamer <Galat...@gmail.comwrote:
I was wondering how to save PHP variables to a txt file and then
retrieve them again.
Example:
There is an input box, after submitted the stuff that was written in
the input box will be saved to a text file. Later on the results need
to be brought back as a variable. So lets say the variable is $text I
need that to be saved to a text file and be able to retrieve it back
again.
Hope it makes sense,
Thanks in advance!!!
I believe serialize is usually used in cases like this:
http://www.php.net/manual/en/function.serialize.php
A while back, I wrote a function called array_smart_dump (yes, the
name makes me giggle, too) that accomplishes the same thing with
arrays a bit more literally.
It will convert an array to a string that, as I put it, is
syntactically usable, meaning you can write the string (enclosed by
appropriate php tags) to file and then later use that array by simply
including the file.
The code can be found here:
http://klenwell.googlecode.com/svn/t...y_smart_dump.i...
Here's a test script:
http://klenwell.googlecode.com/svn/t...s/test.array_s...
Tom

What if I want to simply save one result to a text file and be able to
retrieve it as a variable?
I believe the concept would be the same -- and the execution much
simpler. Something like:

// using heredoc
$file_content = <<<TEXT
<?php

\${$var_name} = {$var_value};

?>
TEXT;

// this would be a user-defined function
write_to_file($fpath, $file_content);

then to retrieve later:

read_file($fpath);

and your variable should be within scope.

Caution with the heredoc above. I don't think the php tags need to be
escaped in any special ways, but test it out -- or use simpler
strings.

Then again, if it's just a single value, why not just write that to
file and then write a simple function to read the file and assign the
return value to the variable you want to use?

Tom

Oct 1 '07 #4

P: n/a
Lamer wrote:
I was wondering how to save PHP variables to a txt file and then
retrieve them again.

Example:

There is an input box, after submitted the stuff that was written in
the input box will be saved to a text file. Later on the results need
to be brought back as a variable. So lets say the variable is $text I
need that to be saved to a text file and be able to retrieve it back
again.
Hope it makes sense,

Thanks in advance!!!
Why are you saving it in a text file? If it's only going to be for a
short time, you should use the $_SESSION superglobal. If it's going to
be a longer time, I recommend a database.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Oct 1 '07 #5

P: n/a
On Sep 30, 9:03 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Lamer wrote:
I was wondering how to save PHP variables to a txt file and then
retrieve them again.
Example:
There is an input box, after submitted the stuff that was written in
the input box will be saved to a text file. Later on the results need
to be brought back as a variable. So lets say the variable is $text I
need that to be saved to a text file and be able to retrieve it back
again.
Hope it makes sense,
Thanks in advance!!!

Why are you saving it in a text file? If it's only going to be for a
short time, you should use the $_SESSION superglobal. If it's going to
be a longer time, I recommend a database.
$_SESSION superglobal?

Oct 1 '07 #6

P: n/a
Lamer wrote:
On Sep 30, 9:03 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>Lamer wrote:
>>I was wondering how to save PHP variables to a txt file and then
retrieve them again.
Example:
There is an input box, after submitted the stuff that was written in
the input box will be saved to a text file. Later on the results need
to be brought back as a variable. So lets say the variable is $text I
need that to be saved to a text file and be able to retrieve it back
again.
Hope it makes sense,
Thanks in advance!!!
Why are you saving it in a text file? If it's only going to be for a
short time, you should use the $_SESSION superglobal. If it's going to
be a longer time, I recommend a database.

$_SESSION superglobal?
Look at the docs.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Oct 1 '07 #7

P: n/a
<comp.lang.php>
<Lamer>
<Mon, 01 Oct 2007 02:30:39 -0000>
<11*********************@y42g2000hsy.googlegroups. com>
I was wondering how to save PHP variables to a txt file and then
retrieve them again.

Example:

There is an input box, after submitted the stuff that was written in
the input box will be saved to a text file. Later on the results need
to be brought back as a variable. So lets say the variable is $text I
need that to be saved to a text file and be able to retrieve it back
again.
$filename="zonoff/demo.php"; $fp=fopen($filename,"w");
fwrite ($fp,"<?php"); fwrite ($fp,"\n");
fwrite ($fp,$james); fwrite ($fp,"\n");
fwrite ($fp,"?>"); fwrite ($fp,"\n");
fclose($fp);

<?php
$filename="zonoff/demo.php"; $fp=fopen($filename,"r");
$qaz=fgets($fp);
$qaz=fgets($fp); $bond=trim($qaz);
fclose($fp);
?>
--
(c) The Amazing Krustov
Oct 1 '07 #8

P: n/a
I was wondering how to save PHP variables to a txt file and then
retrieve them again.
This is obviously serialize, as it's the easiest way of doing this
work:

$stuff_to_store = serialize($myvariable);

$handle = fopen('file.txt','w+');
fwrite($handle, $stuff_to_store);

---

$stuff_i_stored = unserialize(file_get_contents('file.txt'));

;)

Oct 1 '07 #9

P: n/a
On 1 Oct, 12:08, Bruno Barros <rage...@gmail.comwrote:
I was wondering how to save PHP variables to a txt file and then
retrieve them again.

This is obviously serialize, as it's the easiest way of doing this
work:

$stuff_to_store = serialize($myvariable);

$handle = fopen('file.txt','w+');
fwrite($handle, $stuff_to_store);

---

$stuff_i_stored = unserialize(file_get_contents('file.txt'));

;)
Could var_export do something similar?

Oct 1 '07 #10

P: n/a
Could var_export do something similar?

Yes but those things would not be retrievable as easily as with
unserialize ;).

Oct 1 '07 #11

P: n/a
On Mon, 01 Oct 2007 14:03:55 +0200, Bruno Barros <ra*****@gmail.comwrote:
>Could var_export do something similar?

Yes but those things would not be retrievable as easily as with
unserialize ;).
Depends on how much effort you put in writing to the file. If you do it
right, you just include the file and your variable is automagically
available. About saving this information to a file I agree with Jerry
though. Use sessions (which with the default 'files' handler will
automatically take care of those reading/writing issues), or use a
database for longer needed information.
--
Rik Wasmus
Oct 1 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.