<?php
function generateTmpFilename(): string
{
$filename = sys_get_temp_dir().'/'.bin2hex(random_bytes(16)).'.tmp';
touch($filename);
return $filename;
}
$tmpFilename = generateTmpFilename();
$f = fopen($tmpFilename, 'rwb'); # this is the only difference
fwrite($f, 'Woop die loop');
fwrite($f, 'Scoop');
fclose($f);
# Note that this is empty output
var_dump(file_get_contents($tmpFilename));
$tmpFilename = generateTmpFilename();
$f = fopen($tmpFilename, 'wrb'); # this is the only difference
fwrite($f, 'I ate');
fwrite($f, 'Noogie');
fclose($f);
# And this one contains expected text
var_dump(file_get_contents($tmpFilename));