Skip to content

52: Handle Stream Flush and Close Failures

Simulate a buffered write that fails during finalization. PHP 8.6 propagates stream flush and close failures through fclose(), file_put_contents(), and copy().

  • PHP 8.6 CLI. Confirm the version with php --version.
  • allow_url_fopen=1 for the in-memory data URL used as the copy source.
  • Review the previous chapter if you’re following the series in order.

Allow about 15 minutes for the walkthrough and exercise.

A runnable stream-close-failures.php example.

Run the following from the root of the Code with PHP repository.

Terminal window
php code/php-8-6/52-stream-close-failures/stream-close-failures.php
stream-close-failures.php
<?php
declare(strict_types=1);
final class BufferedSink
{
public mixed $context;
public function stream_open(string $path, string $mode, int $options, ?string &$openedPath): bool
{
return true;
}
public function stream_write(string $data): int
{
return strlen($data);
}
public function stream_flush(): bool
{
return false;
}
}
if (!stream_wrapper_register('tutorialsink', BufferedSink::class)) {
throw new RuntimeException('Cannot register test stream');
}
try {
$stream = fopen('tutorialsink://output', 'w');
if ($stream === false) {
throw new RuntimeException('Cannot open test stream');
}
var_dump(fwrite($stream, 'sample'));
var_dump(fclose($stream));
var_dump(file_put_contents('tutorialsink://output', 'sample'));
var_dump(copy('data://text/plain,sample', 'tutorialsink://output'));
} finally {
stream_wrapper_unregister('tutorialsink');
}
int(6)
bool(false)
bool(false)
bool(false)

A successful fwrite() confirms the stream accepted bytes. You must also check finalization before reporting success. Check file_put_contents() with === false so a successful zero-byte result remains valid.

Make the wrapper’s stream_flush() return true. All three operations should then report success, and file_put_contents() should report the number of accepted bytes.

Continue with Handle New ValueErrors When Upgrading.