Is there some url/stream that fopen
will successfully open on most PHP installations? /dev/null
is not available or openable on some systems. Something like php://temp
should be a fairly safe bet, right?
The application for this code that guarantees a file resource, instead of the mixed filetype of bool|resource
you have with fopen
:
/**
* @return resource
*/
function openFileWithResourceGuarantee() {
$fh = @fopen('/write/protected/location.txt', 'w');
if ( $fh === false ) {
error_log('Could not open /write/protected/location.txt');
$fh = fopen('php://temp');
}
return $fh;
}
In PHP 7 with strict types, the above function should guarantee a resource and avoid bools. I know that resources are not official types, but still want to be as type-safe as possible.
php://memory
should be universally available.