Skip to content

FopenBinaryUnsafeUsage ​

warning on by default quick-fix

Group: Compatibility · PhpStorm name: FopenBinaryUnsafeUsageInspection

The PHP manual recommends always opening files with the b (binary) flag for portability; the t (text translation) flag is Windows-only and mangles line endings. Also, b must come after the access letter (rb, wb+, r+b) — a leading or middle b (bw) is wrong.

Example ​

php
<?php
function open_files($path)
{
    $access = 'a';
    $h1 = fopen($path, $access);
    $h2 = fopen($path, "r+");
    $h3 = fopen($path, 'at');
    $h4 = \fopen($path, 'bx');
    $h5 = fopen($path, 'r+b');
    $h6 = fopen($path, 'xtb');
    $h7 = fopen($path, '');
    $h8 = fopen($path);
    $h9 = fopen($path, $_GET['m']);
}
php
<?php
function open_files($path)
{
    $access = 'ab';
    $h1 = fopen($path, $access);
    $h2 = fopen($path, 'rb+');
    $h3 = fopen($path, 'ab');
    $h4 = \fopen($path, 'xb');
    $h5 = fopen($path, 'r+b');
    $h6 = fopen($path, 'xtb');
    $h7 = fopen($path, '');
    $h8 = fopen($path);
    $h9 = fopen($path, $_GET['m']);
}

Reported:

  • line 5: Add the 'b' flag to the mode for binary-safe file access.
  • line 6: Add the 'b' flag to the mode for binary-safe file access.
  • line 7: Use the 'b' flag instead of 't' for binary-safe file access.
  • line 8: Move the 'b' flag to the end of the mode (e.g. 'rb', 'rb+').

Options ​

OptionTypeDefaultEffect
ENFORCE_BINARY_MODIFIER_USAGEbooltrueWhen on, modes lacking b (with or without t) are reported; when off only misplaced b is reported.

Configure ​

In custos.json:

json
{
  "rules": {
    "FopenBinaryUnsafeUsage": {
      "enabled": false,
      "options": {
        "ENFORCE_BINARY_MODIFIER_USAGE": true
      }
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore FopenBinaryUnsafeUsage

/**
 * @noinspection FopenBinaryUnsafeUsageInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.