是否存在 fopen
在大多数 PHP 安装中都能成功打开的 url/stream? /dev/null
在某些系统上不可用或不可打开。像 php://temp
这样的东西应该是一个相当安全的选择,对吧?
此代码的应用程序保证文件资源,而不是使用 fopen
所具有的 bool|resource
混合文件类型:
/**
* @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;
}
在具有严格类型的 PHP 7 中,上述函数应该保证资源并避免 bool。我知道resources are not official types ,但仍然希望尽可能类型安全。
最佳答案
php://memory
应该普遍可用。
关于php - PHP 上的文件资源有保障吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52116166/