Hello,

I am probably missing something but I can not seem to write anything that comes from $_POST to a file. My problem is with the second line below:

<?php
echo $POST['var']; //works fine
file_put_contents('file1.txt',$
POST['var']);//file empty
file_put_contents('file2.txt','some output');//works fine
?>

I read a lot about types, super globals, file_put_contents etc. but there's nothing to help me there. Any help greatly appreciated.

Sirch

    Did you check if file1.txt has write permissions?

      Yes, directory and file have write permissions.
      If I delete file1.txt and file2.txt both are created as expected.
      Only file2.txt has content though.

        This one seems strange.....wouldn't think there would be any type issues. Try assiging the $_POST to a variable. Maybe for some reason it trips up on the array. Possibly a PHP error....

          I have tried this:

          <?php
          $data = $_POST['var'];
          echo $data;//works fine
          file_put_contents ('file1.txt', $data ) ;//file empty
          file_put_contents('file2.txt', 'some output');//works fine
          ?>

          And even this:

          <?php
          $data = $POST['var'];
          $data = "some text".$data;
          echo $data;//works fine
          file_put_contents ('file1.txt', $data ) ;
          //file only contains 'some text', $
          POST['var'] string seems to get stripped first
          file_put_contents('file2.txt', 'some output');//works fine
          ?>

          It almost seems as if file_put_contents() is rerunning the entire php block in a separate session where it does not have access to the $_POST array.

            I have now tried this on 2 more servers and it works fine on them. They are both running versions of Debian linux and apache 1.3.7/php4 and apache 2/php5.

            The machine where it does not work is running windows Xp professional SP1 with apache2 and php5.

            Unfortunately I have no choice but to use the windows machine 🙁
            So any help is still very appreciated 😉

              Tried it on another xp-sp1/apache2/php5 machine: works like a charm
              What can this be?

                What version of PHP does the server have? It might be the version that's outdated.

                  The server is running PHP5

                    5 days later

                    damn, this sucks cuz i would help but i do most of my testing on a windows server (my own box). means that alot of this i'm going to have to redo for *nix machines.

                    i would say try closing the lock on the first file then reopening and closing for the second file

                      The file_put_contents() function is supposed to return the number of bytes written to the file.
                      Have you check it ? Is it 0 ?

                        file_put_contents() returns the correct/expected number of bytes but does not write them to the file

                          Bit of a daft question, but what does $_POST['var'] actually contain? And while I'm asking daft questions, what is the file size of file1.txt afterwards?

                            $_POST['var'] comes from the form below. I'm testing it with 'foobar' but also other (normal) strings.

                            The size of file1.txt is 0 bytes.

                            <form method="POST" action="query.php">
                            <input type="text" name="var">
                            <input type="submit" value="Submit" name="B1">
                            <input type="reset" value="Reset" name="B2"></p>
                            </form>

                              I was kinda hoping there was something wacky that was screwing up whatever you were viewing the text file with (a NUL, say) 🙁

                              I've certainly never seen this before; it's always been, if it has permission to create the file it has permission to write in it. I'd suggest comparing the php.ini on the two XP boxes, but haven't got any suggestions about what to look for (just diff them and see if there are any differences).

                              Which version of PHP exactly are you running (as reported when you run php -v from the command line)? It sounds a bit like this bug....

                                The machine is running XPpro SP1, Apache 2.0.55 and PHP 5.1.2.
                                I will try its php.ini on a machine that does work and compare it.

                                It is echoing and writing okay, if I do this:
                                <?php
                                $data = $_POST['var']."some text";
                                echo file_put_contents ('file1.txt', $data );
                                ?>
                                file_put_contents says it's written 15 bytes, but the file only contains 9 bytes ("some text").

                                If I didn't need it to work it would just be hilarious :rolleyes:

                                  OK, copied the php.ini from the machine that does not work to a machine that does work, restarted apache and... it works.

                                  The problem is very specific to the machine that doesn't work, but why?

                                    I found a workaround. File_put_contents does not seem too convinced that there is any $_post data so I thought this might help:

                                    if ($_POST) file_put_contents ('file1.txt', $data );

                                    and it does. Thank you all for suggestions and help.

                                      Write a Reply...