Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Extra CR's using fputs

    I am trying to write a file out in php (it is actually another php script I am writing out to the local disk).

    I have encoded the data to be written out in base64.

    I then use the following code to write the file :

    Code:
    $fh=fopen("newscript.php","a");
    fputs($fh,base64_decode($newscript));
    fclose($fh);
    However, the resulting file has an extra CR at the end of every line - works fine but just looks terrible LOL!

    Any ideas much appreciated please.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    "line" is related to that $newscript and base64_decode call, not to the put for the file (which hasn't a clue what a linefeed is beyond being a char).
    How is $newscript created?

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    "line" is related to that $newscript and base64_decode call, not to the put for the file (which hasn't a clue what a linefeed is beyond being a char).
    How is $newscript created?
    Basically, it is a script I haved created using Notepad++. I then copy and paste it into this website : http://www.base64encode.org/

    Then I copy and paste the Base64 ecoding back into my "creator" script and assign the variable like so :

    Code:
    $newscript=<<<EOT
    aWYgKGlzc2V0KCRfR0VUWyJzaXRlIl0pKSB7DQoJJHNpdGU9JF9HRVRbInNpdGUiXTsNCgkkc2l0ZXBhdGggPSBkaXJuYW1lKF9fRklMRV9fKSAuICcvJyA7DQoJJHNpdGVwYXRoID0gZGlybmFtZSgkc2l0ZXBhdGgpIC4gJy8nOw0KCSRzaXRlcGF0aCAuPSAkc2l0ZTsNCn0gZWxzZSB7DQoJaGVhZGVyKCJsb2NhdGlvbjptYWluLnBocCIpOw0KfQ==
    EOT;

  • #4
    New Coder
    Join Date
    Nov 2011
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I'm wondering if it could be the heredoc? I am about to try it without.

  • #5
    New Coder
    Join Date
    Nov 2011
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Nope... that didn't work either lol!

  • #6
    New Coder
    Join Date
    Nov 2011
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts
    If I copy the Base64 string back into the base64decode.org website it decodes it perfectly though.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    If you are copying and pasting back from an external source, does it contain the linefeeds when entered? I would expect that would corrupt a base64 encoded string.
    Heredoc won't make a difference except it would contain any linefeeds provided excluding the first and last around the heredoc statement (as per the rules of heredoc).
    Base64 encoded on the other hand will encode any linefeeds as well. So the end result of a base64_decode will also include the same linefeeds. If you don't want them, replace them with nothing either before or after the encode/decode process. A simple str_replace will do that for you.

  • #8
    New Coder
    Join Date
    Nov 2011
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    If you are copying and pasting back from an external source, does it contain the linefeeds when entered? I would expect that would corrupt a base64 encoded string.
    Heredoc won't make a difference except it would contain any linefeeds provided excluding the first and last around the heredoc statement (as per the rules of heredoc).
    Base64 encoded on the other hand will encode any linefeeds as well. So the end result of a base64_decode will also include the same linefeeds. If you don't want them, replace them with nothing either before or after the encode/decode process. A simple str_replace will do that for you.
    Maybe I should just write the code to base64_encode the files rather than using the external website LOL!

    If I copy&paste the base64 encoded string BACK into base64decode.org (the decoding equiv of base64encode.org) it decodes them back again without the additional linefeeds though! Strange!

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Could be the display. If its not in a textarea, HTML ignores whitespace so its certainly possible that its still there.
    There's no reason to consider an external site. I think every language has an ability to convert bases in some fashion or another, so the language itself can be used alone.

  • #10
    New Coder
    Join Date
    Nov 2011
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Could be the display. If its not in a textarea, HTML ignores whitespace so its certainly possible that its still there.
    There's no reason to consider an external site. I think every language has an ability to convert bases in some fashion or another, so the language itself can be used alone.
    I thought that but when I then copy and paste the decoded code from the website into Notepad++ it is perfect.

    It IS something to do with that site however, I have just written a quick script to encode a file with base64_encode and the resulting encoded string works perfectly


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •