0

I'm trying to store data in a text file in a certain format.

Here is the code:

<?php
header ('Location: http://myshoppingsite.com/ ');
$handle = fopen("userswhobought.txt", "a");
foreach($_POST as $variable => $value) {
    fwrite($handle, $variable);
    fwrite($handle, "=");
    fwrite($handle, $value);
    fwrite($handle, "\r\n");
}
fwrite($handle, "===============\r\n");
fclose($handle);
exit;
?>

So on the previous HTML page they put in 2 value, their name and location, then the php code above would get me their info what they inputted and will store it in the userswhobought.txt

This is how it stores at the moment:

Username=John
Location=UK
commit=
===============

But I simply want it to store like this

John:UK
===============
Nextuser:USA
==============
Lee:Ukraine

So it's easier for me to extract.

Thanks

2
  • 1
    Rather than invent your own serialization format, consider saving yourself a lot of pain: use an existing serialization format like xml, json, php's native serialize(), yaml, etc. etc. Consider also databases: sqlite, mysql, bdb... Mar 12, 2013 at 1:32
  • I too recommend using serialize(). The other thing I do especially when the data is already in XML format, is this: file_put_contents($this->pathAndFileName, $xml->asXML());
    – Muskie
    Mar 12, 2013 at 1:42

6 Answers 6

0

take your original code

<?php
header ('Location: http://myshoppingsite.com/ ');
$handle = fopen("userswhobought.txt", "a");
foreach($_POST as $variable => $value) {
    fwrite($handle, $variable);
    fwrite($handle, "=");
    fwrite($handle, $value);
    fwrite($handle, "\r\n");
}
fwrite($handle, "===============\r\n");
fclose($handle);
exit;
?>

and change to

<?php
$datastring = $_POST['Username'].":".$_POST['Location']."
===============\r\n";
file_put_contents("userswhobought.txt",$datastring,FILE_APPEND);
header ('Location: http://myshoppingsite.com/ ');
exit;
?>

Instead of looping through the $_POST data you need to directly manipulate the POST data and then you can use it however you want but I would suggest looking into a database option like mysql, postgres, or sqlite - you can even store data in nosql options like mongodb as well.

0
0
<?php
    header ('Location: http://myshoppingsite.com/ ');
    $handle = fopen("userswhobought.txt", "a");
    fwrite($handle, $_POST['Username']);
    fwrite($handle, ":");
    fwrite($handle, $_POST['Location']);
    fwrite($handle, "===============\r\n");
    fclose($handle);
    exit;
?>
1
  • Hi, it doesn't catch the data which the user has inputted with this code
    – John Kee
    Mar 12, 2013 at 1:40
0
<?php
header ('Location: http://myshoppingsite.com/ ');
$handle = fopen("userswhobought.txt", "a");
foreach($_POST as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, ":");
fwrite($handle, $value);
fwrite($handle, "===============\r\n");
}

fclose($handle);
exit;
?>
0
foreach($_POST as $variable => $value) {
    $write_this = "$variable:$value\r\n"
    fwrite($handle, $write_this );
}
fwrite($handle, "===============\r\n");

Additionally, I would suggest moving the header() call to right before the exit. Technically, that works, but it's not what most people do.

0

Instead of your foreach, just add $_POST['Username'].":".$_POST['Location']."\r\n" in your file.

0

Just place fwrite($handle, "===============\r\n"); inside your loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.