I am experiencing a weird issue that I can't explain, hoping you can help me with this.

I am writing the contents of a form to a file like this:

			$data = stripslashes($_POST['data']);
			// create external data file
			$file = fopen($filepath, 'w+') or die("ERROR: Can't create/edit file. Check permissions!");
			fwrite($file, $data);
			chmod($filepath,0666);
			fclose($file);
			// create external data file backup
			$file = fopen($filepath.'.bak', 'w+') or die("ERROR: Can't create/edit backup file. Check permissions!");
			fwrite($file, $data);
			chmod($filepath,0666);
			fclose($file);

As you can see the two sections of the code are identical (except for the addition of the "bak" extension for the backup file) however the two files that are generated are different...

The first file contains an extra empty line after every existing line, while the second file (the backup one) contains exactly what is sent in the $data variable via post...

How is this possible? I can't explain it.
Any ideas?

Thanks

    Unrelated, by why are you calling [man]stripslashes/man on the incoming data? There shouldn't be any need for this.

    As for your problem, note that the behavior itself is not that strange or unexpected; it sounds a lot like the 't' mode has been enabled. To combat this, try adding the 'b' flag on both fopen() calls and see if the resulting files are consistent with each other.

      Sorry but what do you mean by t mode and b flag? I looked on the PHP manual at http://php.net/manual/en/function.fopen.php and I don't see any mentions of those... All I see are the r,w,a,x,c modifiers... I'm confused...

      I had to add stripslashes because without it it was adding slashes to all the ' symbols (single quotes)

        marcnyc;10996975 wrote:

        Sorry but what do you mean by t mode and b flag? I looked on the PHP manual at http://php.net/manual/en/function.fopen.php and I don't see any mentions of those...

        Really? That seems strange, considering all three 'Notes' in the section that talks about the mode parameters mention at least one of those modes. Perhaps you should take another look (especially at that part of the page)?

        marcnyc;10996975 wrote:

        I had to add stripslashes because without it it was adding slashes to all the ' symbols (single quotes)

        No, what you needed to do (or, rather, still need to do) is disable the (highly) deprecated magic_quotes_gpc PHP directive (and then go find the person who enabled it and make sure they never get close to PHP or a webserver ever again :p). See the manual page [man]security.magicquotes[/man] for more info.

          I stand corrected. I read the notes and tried appending b at the end of the mode but it did not work.
          I initially tried on the fopen that writes:

          fopen($filepath, 'w+b')

          then, unsure of why it didn't work, I tried it on the fopen that reads:

          fopen($filepath, 'rb')

          but still no luck.

          Did I put the b in the right place? The notes say to put it as the last character of the mode parameter.

          And as for magic_quotes, correct me if I am wrong, but that seems to be a server admin thing, not a webmaster thing... since I am on a shared host I don't think I can change that, can I?

            marcnyc;10996978 wrote:

            I tried it on the fopen that reads:

            fopen($filepath, 'rb')

            but still no luck.

            Er.. what "fopen that reads" are you referring to? Have you tried verifying whether these empty lines are actually present in the file(s) by viewing the contents of the file(s) directly?

            marcnyc;10996978 wrote:

            Did I put the b in the right place? The notes say to put it as the last character of the mode parameter.

            Well then answer me this: did you put it as the last character of the mode parameter? 😉

            marcnyc;10996978 wrote:

            And as for magic_quotes, correct me if I am wrong, but that seems to be a server admin thing, not a webmaster thing... since I am on a shared host I don't think I can change that, can I?

            The fact that you aren't the "server admin" or that you are on a shared host does not mean you shouldn't be able to alter (most of) the PHP configuration; see [man]configuration.changes[/man] for more info.

            And besides... if I was on a host that has magic_quotes enabled, the first thing I would do is immediately look for a competent host and transfer all of my websites/accounts immediately. 😉

              bradgrafelman;10996979 wrote:

              Er.. what "fopen that reads" are you referring to? Have you tried verifying whether these empty lines are actually present in the file(s) by viewing the contents of the file(s) directly?

              The file that i am writing with the portion of the code I posted in my original post is a file that I am opening earlier in my code so that I can modify the file.

              Yes I have verified the empty lines by downloading the newly saved file via FTP and opening it with BBEdit (on Mac).

              bradgrafelman;10996979 wrote:

              Well then answer me this: did you put it as the last character of the mode parameter? 😉

              I think I did. I showed you what I did. I added it after 'w+' so it became 'w+b'. Is that wrong?

              bradgrafelman;10996979 wrote:

              The fact that you aren't the "server admin" or that you are on a shared host does not mean you shouldn't be able to alter (most of) the PHP configuration; see [man]configuration.changes[/man] for more info.

              And besides... if I was on a host that has magic_quotes enabled, the first thing I would do is immediately look for a competent host and transfer all of my websites/accounts immediately. 😉

              I'll look into this and I am sure you have your reasons but I have been very happy with current host and I am a bit reluctant to make changes like those because I don't fully understand them (I am not a system admin, just a hobby coder) and it might mean for me having to change/rewrite lots of code that I have tested on the current configuration.
              You are probably right but it is something I wasn't aware of when I chose the host and having come from bad hosting experiences I've been very satisfied so far with my host.

                Do you guys have any other ideas about this? I still cannot figure out why the two identical fopens() generate different looking files

                  What are the sizes of the two files (in bytes)?

                    They are both 6114 bytes but one has spaces and the other one doesn't

                      Have you tried removing the .bak extension and then viewing it? It's possible the editor you are using is interpretting \r\n as 2 lines with .bak where as its seeing it as one for the regular extension.

                        wow you are right Derokorian! never knew that!
                        Indeed if I remove the .bak extension the spaces are added to that file as well!

                        That solves the mystery of why they look different.
                        Now the only mystery that remains is why these spaces are added in the first place!
                        Any ideas on that?

                          This is really dependent entirely on the program you use to open the files. There are no spaces being added or removed. However the characters are being interpreted differently in the known file type vs. the unknown file type.

                            marcnyc;10997245 wrote:

                            They are both 6114 bytes but one has spaces and the other one doesn't

                            That's impossible. "Space" characters consume space (e.g. the room required to store the data to disk) just like any other character. In the end, it's all just binary 1's and 0's.

                              bradgrafelman;10997256 wrote:

                              That's impossible. "Space" characters consume space (e.g. the room required to store the data to disk) just like any other character. In the end, it's all just binary 1's and 0's.

                              Apparently, as Derokorian help me realize, the files are the same but they are being read differently by the same program.

                              Derokorian;10997256 wrote:

                              This is really dependent entirely on the program you use to open the files. There are no spaces being added or removed. However the characters are being interpreted differently in the known file type vs. the unknown file type.

                              How is it possible that they are being read differently by the same software?

                                marcnyc;10997318 wrote:

                                How is it possible that they are being read differently by the same software?

                                In windows, line breaks are composed of two ASCII characters - a carriage return followed by a line feed, commonly referred to as CRLF or written as "\r\n" in code.

                                In Unix/Linux, however, line breaks consist of only one of those characters - the line feed, commonly referred to as LF or "\n" in code.

                                Perhaps the editor is somehow translating the line breaks incorrectly and showing duplicate line breaks? It would possibly help to know what you're using to view these files.

                                  Because "most" text editing software makes "guesses" about how to display the document your viewing based on the extension. 2 different extensions could mean two different interpretations of in this case the carriage return line feed (I imagine).

                                  Edit: Damn didn't see page 2

                                    bradgrafelman;10997319 wrote:

                                    In windows, line breaks are composed of two ASCII characters - a carriage return followed by a line feed, commonly referred to as CRLF or written as "\r\n" in code.

                                    In Unix/Linux, however, line breaks consist of only one of those characters - the line feed, commonly referred to as LF or "\n" in code.

                                    Perhaps the editor is somehow translating the line breaks incorrectly and showing duplicate line breaks? It would possibly help to know what you're using to view these files.

                                    I believe I said in an earlier post that I am using BBEdit.

                                    I kinda know how the spacing works but I still don't know why the two files are being read differently by the same software... anyway, at this point, it's all working, I am just curious

                                      marcnyc wrote:

                                      I am just curious

                                      Based on the file extension, BBEdit identifies the first file as a text document, guesses from its use of CRLF that it uses Windows/network line endings, and displays it treating CRLF as a single line break.

                                      Based on the file extension (".bak"), BBEdit doesn't identify the second file as a text document, and doesn't do any work to identify what sort of line breaks the document uses.

                                        so, out of curiosity, does that mean there are really two empty lines in the file even if I see one?