...

View Full Version : Extra CR's using fputs



spman
11-13-2012, 07:58 PM
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 :


$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.

Fou-Lu
11-13-2012, 08:02 PM
"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?

spman
11-13-2012, 08:18 PM
"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 :


$newscript=<<<EOT
aWYgKGlzc2V0KCRfR0VUWyJzaXRlIl0pKSB7DQoJJHNpdGU9JF9HRVRbInNpdGUiXTsNCgkkc2l0ZXBhdGggPSBkaXJuYW1lKF9f RklMRV9fKSAuICcvJyA7DQoJJHNpdGVwYXRoID0gZGlybmFtZSgkc2l0ZXBhdGgpIC4gJy8nOw0KCSRzaXRlcGF0aCAuPSAkc2l0 ZTsNCn0gZWxzZSB7DQoJaGVhZGVyKCJsb2NhdGlvbjptYWluLnBocCIpOw0KfQ==
EOT;

spman
11-13-2012, 08:26 PM
I'm wondering if it could be the heredoc? I am about to try it without.

spman
11-13-2012, 08:31 PM
Nope... that didn't work either lol!

spman
11-13-2012, 08:35 PM
If I copy the Base64 string back into the base64decode.org website it decodes it perfectly though.

Fou-Lu
11-13-2012, 08:37 PM
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.

spman
11-13-2012, 08:41 PM
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!

Fou-Lu
11-13-2012, 08:49 PM
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.

spman
11-13-2012, 09:04 PM
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 :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum