1.2.468. Fopen Binary Mode

Use explicit b when opening files.

fopen() supports a b option in the second parameter, to make sure the read is binary. This is the recommended way when writing portable applications, between Linux and Windows. Also, Windows PHP does support a t option, that translates automatically line endings to the right value. As this is Windows only, this should be avoided for portability reasons.

<?php

// This opens file with binary reads on every OS
$fp = fopen('path/to/file.doc', 'wb');

// This may not open files with binary mode on Windows
$fp = fopen('path/to/file.doc', 'w');

?>

See also fopen.

1.2.468.1. Suggestions

  • Add the b option when opening files

1.2.468.2. Specs

Short name

Portability/FopenMode

Rulesets

All

Exakat since

0.8.4

PHP Version

All

Severity

Minor

Time To Fix

Instant (5 mins)

Precision

Very high

Features

file

Available in

Entreprise Edition, Exakat Cloud