Question re writing to a text file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JackM

    Question re writing to a text file

    I am attempting to code a small script to process the fields from a form
    and write it to a text file using the semi-colon as the delimiter. So
    far I am successful in doing that using the following:

    $incoming_field s = array_keys($_PO ST);
    $incoming_value s = array_values($_ POST);

    for ($i = 0; $i < count($incoming _fields); $i++) {

    $fp = fopen("formresu lts.txt", "a");
    fwrite($fp,stri pslashes("$inco ming_values[$i];"));
    fclose($fp);
    }

    But what I am encountering difficulty in is putting a line break (\n) at
    the end of only the final entry so that the data from each form
    submission is on a separate line. How do I approach doing this?
  • Michael Fesser

    #2
    Re: Question re writing to a text file

    .oO(JackM)
    [color=blue]
    >I am attempting to code a small script to process the fields from a form
    >and write it to a text file using the semi-colon as the delimiter. So
    >far I am successful in doing that using the following:
    >
    >$incoming_fiel ds = array_keys($_PO ST);
    >$incoming_valu es = array_values($_ POST);
    >
    >for ($i = 0; $i < count($incoming _fields); $i++) {
    >
    >$fp = fopen("formresu lts.txt", "a");
    >fwrite($fp,str ipslashes("$inc oming_values[$i];"));
    >fclose($fp);
    >}[/color]

    Uh, that looks bad. You open and close the file over and over again,
    that's not necessary. The calls to array_keys() and array_values() are
    not necessary as well and stripslashes() should not be used on posted
    variables without checking the setting of magic quotes first.
    [color=blue]
    >But what I am encountering difficulty in is putting a line break (\n) at
    >the end of only the final entry so that the data from each form
    >submission is on a separate line. How do I approach doing this?[/color]

    Try this:

    $data = implode(';', $_POST);
    if (get_magic_quot es_gpc()) {
    $data = stripslashes($d ata);
    }
    $fp = fopen('formresu lts.txt', 'a');
    fwrite($fp, "$data\n");
    close($fp);

    Micha

    Comment

    • JackM

      #3
      Re: Question re writing to a text file

      Michael Fesser wrote:[color=blue]
      > .oO(JackM)
      >
      >[color=green]
      >>I am attempting to code a small script to process the fields from a form
      >>and write it to a text file using the semi-colon as the delimiter. So
      >>far I am successful in doing that using the following:
      >>
      >>$incoming_fie lds = array_keys($_PO ST);
      >>$incoming_val ues = array_values($_ POST);
      >>
      >>for ($i = 0; $i < count($incoming _fields); $i++) {
      >>
      >>$fp = fopen("formresu lts.txt", "a");
      >>fwrite($fp,st ripslashes("$in coming_values[$i];"));
      >>fclose($fp) ;
      >>}[/color]
      >
      >
      > Uh, that looks bad. You open and close the file over and over again,
      > that's not necessary.[/color]

      Oh, I wasn't aware that it would open, write and close the file for
      *each* value. I thought it would write them all while the file was open,
      then close. Thanks for that information.

      [color=blue][color=green]
      >>But what I am encountering difficulty in is putting a line break (\n) at
      >>the end of only the final entry so that the data from each form
      >>submission is on a separate line. How do I approach doing this?[/color]
      >
      >
      > Try this:
      >
      > $data = implode(';', $_POST);
      > if (get_magic_quot es_gpc()) {
      > $data = stripslashes($d ata);
      > }
      > $fp = fopen('formresu lts.txt', 'a');
      > fwrite($fp, "$data\n");
      > close($fp);[/color]

      Thank you. Much more compact than what I had written and works fine once
      I corrected the close. ;) Much obliged for the information and the help.

      Comment

      • Michael Fesser

        #4
        Re: Question re writing to a text file

        .oO(JackM)
        [color=blue]
        >Oh, I wasn't aware that it would open, write and close the file for
        >*each* value. I thought it would write them all while the file was open,
        >then close. Thanks for that information.[/color]

        The fopen/fwrite/fclose was inside the loop, so it was executed for
        every single value.
        [color=blue]
        >Thank you. Much more compact than what I had written and works fine once
        >I corrected the close.[/color]

        Oops ... typo. ;)

        Micha

        Comment

        Working...