Multiple File Functions Reject NUL Bytes In Filenames
Several filesystem functions, such as file_exists(), used to accept a filename containing a NUL byte; the string was silently truncated by the underlying C library call, or the argument was simply rejected with a warning and a NULL/false return value. In PHP 8.6, a NUL byte in the filename argument of these file functions throws a ValueError instead.
PHP code
<?php
try {
var_dump(file_exists("/tmp/foo\0bar"));
} catch (\ValueError $e) {
echo $e->getMessage(), "\n";
}
?>
Before
PHP Warning: file_exists() expects parameter 1 to be a valid path, string given in /codes/fileFunctionsNullByteValueError.php on line 4
Warning: file_exists() expects parameter 1 to be a valid path, string given in /codes/fileFunctionsNullByteValueError.php on line 4
NULL
After
file_exists(): Argument #1 ($filename) must not contain any null bytes
PHP version change
This behavior changed in 8.6.