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

John Kee
  • 45
  • 1
  • 3
  • 10
  • 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... – Frank Farmer Mar 12 '13 at 01: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 '13 at 01:42

6 Answers6

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;
?>
Tushar
  • 8,019
  • 31
  • 38
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;
?>
u54r
  • 1,767
  • 2
  • 15
  • 26
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.

Trenton Trama
  • 4,890
  • 1
  • 22
  • 27
0

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

Quanturium
  • 5,698
  • 2
  • 30
  • 36
0

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

afarazit
  • 4,907
  • 2
  • 27
  • 51
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.

Geek Num 88
  • 5,264
  • 2
  • 22
  • 35