Save multiple variables to text file with php

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
bocaj812
Forum Newbie
Posts: 4
Joined: Wed Aug 05, 2009 10:49 am
Location: n/a
Contact:

Save multiple variables to text file with php

Post by bocaj812 »

I need to be able to save form data to a .txt file with php.

I have three variables. $title, $description, and $content.

I need to save them to the .txt file, and be able to open that .txt file with php and use all three of the variables.

thanx,
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Save multiple variables to text file with php

Post by Mark Baker »

Then take a look at functions like fopen(), fwrite(), fclose() and fgets()
bocaj812
Forum Newbie
Posts: 4
Joined: Wed Aug 05, 2009 10:49 am
Location: n/a
Contact:

Re: Save multiple variables to text file with php

Post by bocaj812 »

Mark Baker wrote:Then take a look at functions like fopen(), fwrite(), fclose() and fgets()
thanks

I looked at that, but it appears to only write one variable and i'm not sure if I could write multiple variable sand still use all three when i open the file up again.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Save multiple variables to text file with php

Post by Mark Baker »

bocaj812 wrote:I looked at that, but it appears to only write one variable and i'm not sure if I could write multiple variable sand still use all three when i open the file up again.
Well, you could use three write statements, one for each variable, or a single write that wrote all three variables concatenated together.
Alternatively, you could store each of the three variables in an array, then serialise the array and write the result.... then unserialize it when you read it again.
bocaj812
Forum Newbie
Posts: 4
Joined: Wed Aug 05, 2009 10:49 am
Location: n/a
Contact:

Re: Save multiple variables to text file with php

Post by bocaj812 »

Mark Baker wrote: you could store each of the three variables in an array, then serialise the array and write the result.... then unserialize it when you read it again.
thanks i'll try that
bocaj812
Forum Newbie
Posts: 4
Joined: Wed Aug 05, 2009 10:49 am
Location: n/a
Contact:

Re: Save multiple variables to text file with php

Post by bocaj812 »

I can put my variables into an array but now, how do i get them out?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Save multiple variables to text file with php

Post by Jonah Bron »

The fread() function. Then unserialize() the result like Mark said.
Post Reply