473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Save PHP variables to a text file

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
11 26069
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
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
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
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
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
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
<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
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
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
Could var_export do something similar?

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

Oct 1 '07 #11
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Thomas | last post by:
Hi there, I am trying to store data in a text file and output it to the browser using PHP. All very easy - if I was using English! the problem is, I want to use Japanese and I'm finding it a tad...
3
by: Dynamo | last post by:
Hi all I want to save the contents of a textarea within my webform as a text file .txt on my webserver. In addition, I want to save it with the same file name as the next autoincrement number in...
0
by: Alex Smith | last post by:
Hi Friends, I want to open and read a pdf file from disk And save this content of pdf file into .txt file. In short I want to convert content of pdf into .txt formate and save this text file on...
4
by: Ben | last post by:
Hello, I need to save some information into a text file. For this, I use : FileOpen(1, "c:\MyFile.txt", OpenMode.Output) For I = 0 To iCpt - 1 WriteLine(1,...
11
by: Thomas Magma | last post by:
I have a simple JavaScript application that will generate some text type data for the end user. Is it possible to have a button that will allow them to save this information to a text file? ...
2
by: nuhura01 | last post by:
Hi.. I'm trying to save query path which is in text box to text file. Below is the coding that i'm using currently: Private Sub SaveQueryPath() 'save to text file Response.Clear()...
1
by: papafreebird | last post by:
I would like to save the contents of a wxListBox to a text file. Using borland c++ builder I am able to accomplish this by using the following code ListBox1->Items->GetText(); ...
3
by: lerougegorge | last post by:
When I pull a string from a UTF-8 encoded field in my SQL, I get a string like the following: "& #2342;& #2369;& #2312;" (I have inserted a space between "&" and "#" to stop this from...
1
by: AccessHunter | last post by:
Hi, I have written a code to display the Save As dialog box for my users, with a default file name. The code works and displays the dialog box with the default name. But when I click the Save...
1
by: MichaelMelaku | last post by:
I am trying to save a txt file onto the client local drive (c:\Temp.txt) which will contain only xy coordinates generated by my web app. This txt file is to be consumed by another desktop app...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.