awm*_*awm 5 php temporary-files
当tmp目录已满时,file_put_contents返回FALSE,但创建的文件大小为0. file_put_contents应该完成文件的创建或根本不起作用.例如:
$data = 'somedata';
$temp_name = '/tmp/myfile';
if (file_put_contents($temp_name, $data) === FALSE) {
// the message print that the file could not be created.
print 'The file could not be created.';
}
Run Code Online (Sandbox Code Playgroud)
但是当我进入tmp目录时,我可以找到在大小为0的目录中创建的文件"myfile".这使得难以维护.不应该创建该文件,我希望看到tmp目录已满的消息或警告.我错过了什么吗?这是正常的行为吗?
您可能会忽略,如果您执行错误消息,您也需要处理这种情况:
$data = 'somedata';
$temp_name = '/tmp/myfile';
$success = file_put_contents($temp_name, $data);
if ($success === FALSE)
{
$exists = is_file($temp_name);
if ($exists === FALSE) {
print 'The file could not be created.';
} else {
print 'The file was created but '.
'it could not be written to it without an error.';
}
}
Run Code Online (Sandbox Code Playgroud)
这也将允许您处理它,例如在写入临时文件的事务失败时进行清理,以将系统重置回之前的状态。