php - PHP 上的文件资源有保障吗?

标签 php stream disaster-recovery

是否存在 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/

相关文章:

c# - 使用 Compression.DeflateStream 压缩和解压缩 Stream

c++ - stream seekg() 实现从 VS2012 到 2010 各不相同

amazon-web-services - AWS DynamoDB 流 : Can I keep records for longer than 24 hours?

php - 在 html 中显示错误消息

php - Symfony 查找器 : Finding all directories except ones that start with an underscore

PHP解码json并显示

azure - 是否可以使用不同的可启动 iso 启动 Azure VM?

php - C++ 中的 Linux 守护进程处理 PHP 请求

r - 将新数据 append 到 R 中的现有数据框 (RDS)

mysql - 恢复 MySQL 表(MyISAM 和 InnoDB)所需的最小文件集