Save PHP variables to a text file

Save PHP variables to a text file

am 01.10.2007 04:30:39 von Galatorg

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!!!

Re: Save PHP variables to a text file

am 01.10.2007 05:18:04 von klenwell

On Sep 30, 7:30 pm, 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!!!

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/trunk/PHP/array/array_sma rt_dump.inc.php

Here's a test script:

http://klenwell.googlecode.com/svn/trunk/PHP/_unit_tests/tes t.array_smart_dump.php

Tom

Re: Save PHP variables to a text file

am 01.10.2007 05:31:34 von Bryan

On Sep 30, 8:18 pm, klenwell wrote:
> On Sep 30, 7:30 pm, 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!!!
>
> 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/trunk/PHP/array/array_sma rt_dump.i...
>
> Here's a test script:
>
> http://klenwell.googlecode.com/svn/trunk/PHP/_unit_tests/tes t.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?

Re: Save PHP variables to a text file

am 01.10.2007 05:59:44 von klenwell

On Sep 30, 8:31 pm, Bryan wrote:
> On Sep 30, 8:18 pm, klenwell wrote:
>
>
>
> > On Sep 30, 7:30 pm, 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!!!
>
> > 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/trunk/PHP/array/array_sm art_dump.i...
>
> > Here's a test script:
>
> >http://klenwell.googlecode.com/svn/trunk/PHP/_unit_tests/te st.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 = <<
\${$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

Re: Save PHP variables to a text file

am 01.10.2007 06:03:27 von Jerry Stuckle

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.
jstucklex@attglobal.net
==================

Re: Save PHP variables to a text file

am 01.10.2007 06:19:20 von Galatorg

On Sep 30, 9:03 pm, Jerry Stuckle wrote:
> 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?

Re: Save PHP variables to a text file

am 01.10.2007 06:30:49 von Jerry Stuckle

Lamer wrote:
> On Sep 30, 9:03 pm, Jerry Stuckle wrote:
>> 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.
jstucklex@attglobal.net
==================

Re: Save PHP variables to a text file

am 01.10.2007 12:51:07 von Hans-Peter Sauer




<1191205839.552662.43890@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," fwrite ($fp,$james); fwrite ($fp,"\n");
fwrite ($fp,"?>"); fwrite ($fp,"\n");
fclose($fp);

$filename="zonoff/demo.php"; $fp=fopen($filename,"r");
$qaz=fgets($fp);
$qaz=fgets($fp); $bond=trim($qaz);
fclose($fp);
?>


--
(c) The Amazing Krustov

Re: Save PHP variables to a text file

am 01.10.2007 13:08:43 von ragearc

> > 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'));

;)

Re: Save PHP variables to a text file

am 01.10.2007 13:41:53 von Captain Paralytic

On 1 Oct, 12:08, Bruno Barros wrote:
> > > 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?

Re: Save PHP variables to a text file

am 01.10.2007 14:03:55 von ragearc

> Could var_export do something similar?

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

Re: Save PHP variables to a text file

am 01.10.2007 15:09:43 von luiheidsgoeroe

On Mon, 01 Oct 2007 14:03:55 +0200, Bruno Barros wrote:

>> 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