Binary mode with fopen 
Author Message
 Binary mode with fopen

I built a quick PHP script to display a random image from a directory.  I
had previously written a function filetostring () for another project that I
recycled for this one.  The program works by reading an image file into a
string and then echoing it to the browser.

The functions were originally written to handle text functions so the first
attempt resulted in only a small portion of the image in question being
echoed to the browser.  However by adding "b" to the file open mode I was
able to make it work as intended.  I am developing on a Windows environment.

I was wondering, however, would there be any negative consequences of
opening a test file in binary mode?  I figured I could just always open
files in binary mode regardless of weather they were text or binary data.
If not then a minor rewrite of the filetostring function would be in order
as I'd need to pass it an extra parameter to tell it which mode to open in.
Can I read and write text files as binary? Or will this result in corrupted
files?



Mon, 30 May 2005 01:09:50 GMT  
 Binary mode with fopen

Quote:

> I was wondering, however, would there be any negative consequences of
> opening a test file in binary mode?  I figured I could just always open
> files in binary mode regardless of weather they were text or binary data.

Reading a text file in binary mode might affect the way it interprets the
newline characters at the end of each line (i haven't worked under Windows
for so long that i've forgotten if this is a problem). Don't write text in
binary mode (on a Windows machine, at least) or else your end-of-line
sequences will be hosed (nothing tragic, though). Don't ever write [to] a
binary file (.jpg, for example) in text mode, or it will become useless.

Quote:
> If not then a minor rewrite of the filetostring function would be in order
> as I'd need to pass it an extra parameter to tell it which mode to open
> in. Can I read and write text files as binary? Or will this result in
> corrupted files?

Be aware that whether or not string functions are binary-safe depends on
your PHP version. It appears, though the docs are not 100% clear on this
point, that string functions in PHP 4.2+ are binary safe, while at least
*some* of the functions are not bin-safe in 4.1 and earlier.

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.



Mon, 30 May 2005 01:25:27 GMT  
 Binary mode with fopen


Quote:
> I was wondering, however, would there be any negative consequences of
> opening a test file in binary mode?  I figured I could just always
> open files in binary mode regardless of weather they were text or
> binary data. If not then a minor rewrite of the filetostring function
> would be in order as I'd need to pass it an extra parameter to tell it
> which mode to open in. Can I read and write text files as binary? Or
> will this result in corrupted files?

It's fine to write to text files in binary mode, won't mess up the output.

KAH



Mon, 30 May 2005 02:36:17 GMT  
 Binary mode with fopen


Quote:
> It's fine to write to text files in binary mode, won't mess up the output.

I did a little experimenting, trying to write text in binary mode always
resulted in a 0 byte file.  Reading worked fine in binary mode for both text
and binary data.  I'm using PHP 4.2.3 under Apache and Windows XP.  The site
that will host the finished product is a Limux box with PHP 4.1.2.


Mon, 30 May 2005 08:44:10 GMT  
 Binary mode with fopen

Quote:

> I was wondering, however, would there be any negative consequences
> of opening a test file in binary mode?  I figured I could just
> always open files in binary mode regardless of weather they were
> text or binary data.  If not then a minor rewrite of the
> filetostring function would be in order as I'd need to pass it an
> extra parameter to tell it which mode to open in.  Can I read and
> write text files as binary? Or will this result in corrupted files?

The fopen() and fread() etc is closely mapped to the same standard
C-functions. A quick check in 'man fopen' tells us this:

  The mode string can also include the letter ``b'' either as a last
  character or as a character between the characters in any of the
  two- character strings described above.  This is strictly for
  compatibility with ANSI X3.159-1989 (``ANSI C'') and has no effect;
  the ``b'' is ignored on all POSIX conforming systems, including
  Linux.  (Other systems may treat text files and binary files
  differently, and adding the ``b'' may be a good idea if you do I/O
  to a binary file and expect that your program may be ported to
  non-Unix environments.)

So it is probably a good idea to include 'b' whenever you are reading
and writing to a file. Without it, windows clib fread/fwrite() will
completely mess up all your end-of-line chars ('\n') and replace them
with it's own abomination ('\r\n'). This has bitten me a couple of
times and still scratch my head when it happens.

Anyway, the documentation for fopen() says this:

  Note: The mode may contain the letter 'b'. This is useful only on
  systems which differentiate between binary and text files
  (i.e. Windows. It's useless on Unix). If not needed, this will be
  ignored. You are encouraged to include the 'b' flag in order to make
  your scripts more portable.

Which essentially is the same text as in the the documentation for
clib fopen().



Tue, 31 May 2005 05:51:28 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Set standard output to binary mode? (Windows)

2. GST - TCP.Socket binary mode?

3. awk on Microsoft OSs: Binary mode files??

4. writing files binary mode

5. Reading Standard Input in binary mode

6. How to set binary mode to printer

7. open a file in binary mode?

8. writing in a file as binary mode????

9. re-opening stdin in raw (binary) mode?

10. binary mode std(in/out) (Win32)

11. Change standard output to binary mode (Windows)

 

 
Powered by phpBB® Forum Software