fopen binary file + creating properties file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vori
Forum Newbie
Posts: 2
Joined: Fri Nov 07, 2008 8:49 am

fopen binary file + creating properties file

Post by vori »

Hi all.
I have two questions for which I wasn't able to find the answer on the net.

1.
What exactly is difference when using b flag with fopen.
Whatever I do I always get the same result wheter I use b flag or not with fopen, fwrite, fread, pack, unpack.

2.
Is there a function in PHP that allows creating preoperties/ini file like enter/edit key-vale pairs or sections?

Thanks in advance.
Hannes2k
Forum Contributor
Posts: 102
Joined: Fri Oct 24, 2008 12:22 pm

Re: fopen binary file + creating properties file

Post by Hannes2k »

Hi,
Note: Different operating system families have different line-ending conventions. When you write a text file and want to insert a line break, you need to use the correct line-ending character(s) for your operating system. Unix based systems use \n as the line ending character, Windows based systems use \r\n as the line ending characters and Macintosh based systems use \r as the line ending character.
If you use the wrong line ending characters when writing your files, you might find that other applications that open those files will "look funny".
Windows offers a text-mode translation flag ('t') which will transparently translate \n to \r\n when working with the file. In contrast, you can also use 'b' to force binary mode, which will not translate your data. To use these flags, specify either 'b' or 't' as the last character of the mode parameter.
The default translation mode depends on the SAPI and version of PHP that you are using, so you are encouraged to always specify the appropriate flag for portability reasons. You should use the 't' mode if you are working with plain-text files and you use \n to delimit your line endings in your script, but expect your files to be readable with applications such as notepad. You should use the 'b' in all other cases.
If you do not specify the 'b' flag when working with binary files, you may experience strange problems with your data, including broken image files and strange problems with \r\n characters

http://de3.php.net/manual/en/function.fopen.php
vori
Forum Newbie
Posts: 2
Joined: Fri Nov 07, 2008 8:49 am

Re: fopen binary file + creating properties file

Post by vori »

Thank you very much for your answer but I have already read that quote.
I have also read that t flag is bugged somehow.

I quess if I use PHP to both write and read files I should not expect any problems and then it doesn't metter if I use b flag or not.

I f I understood correctly this flag might be of usefull if I try to work with some file that was created by some other text editor or if I am creating file that should be readable by other programs.

I would reeealy like to have one or two concrete examples of what might go wrong and how exactly this flag plays a roll in such examples.

I can only learn from examples.
Pure theory slides by. :)
Post Reply