[thelist] PHP help please- parsing text to variables
Kelly Hallman
khallman at wrack.org
Wed Mar 19 18:28:28 CST 2003
On Wed, 19 Mar 2003, Todd wrote:
> Help!!!! I'm trying to save a form. So I write the $http_post_vars to a
> text document now how do I get them out?
>
> //write function
> $filename ="form.inc";
> $handle= fopen($filename,'w+');
> fputs($handle, $HTTP_POST_VARS);
> fclose($handle);
>
> //read
> $filename ="form.inc";
> $handle = fopen($filename,"r");
> $contents = fread($handle, filesize($filename));
If $HTTP_POST_VARS were a string or similar variable, this code would
probably work just fine. Since $HTTP_POST_VARS is an array, it won't.
Probably the easiest way to do exactly what you're trying to do is with
these two complimentary functions I wrote recently for a similar task:
function shelve($fname,$obj) {
$fp = fopen($fname,"w");
$x = fwrite($fp,serialize($obj));
fclose($fp);
return $x; }
function unshelve($fname) {
$fp = fopen($fname,"r");
$x = unserialize(fread($fp,filesize($fname)));
fclose($fp);
return $x; }
Note that the only major difference is serialize/unserialize, which
convert more complex data structures (such as lists, arrays and even
objects) to and from a string. So all you need to do is call them:
shelve("filename.txt",$HTTP_POST_VARS);
$postvars = unshelve("filename.txt");
> echo "$month $body1 $header1 $body2 $header2";
Also note that this won't work in either case, you need to refer to
elements of the associative array after it's unserialized:
$postvars["month1"] after you restore the array from the file.
--
Kelly Hallman
http://wrack.org/
More information about the thelist
mailing list