当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.';
}
但是当我进入tmp目录时,可以在目录中找到大小为0的文件“ myfile”。这使维护变得很困难。不应创建该文件,并且我想看到一条消息或警告tmp目录已满。我有什么想念的吗?这是正常行为吗?
if(!file_put_contents($temp_name, $data)){
print 'The file could not be created.';
if(file_exists ($temp_name))
unlink($temp_name);
}
$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.';
}
}
这还可以让您处理它,就像在写入临时文件的事务失败的情况下进行清理一样,将系统重新设置为以前的状态。
我很久以来一直在搜索此代码。
$data = 'somedata';
$temp_name = '/tmp/myfile';
$success = file_put_contents($temp_name, $data);
if (!$success){
$exists = is_file($temp_name);
if (!$exists) {
print 'The file could not be created.';
} else {
print 'The file was created but '.
'it could not be written to it without an error.';
}
}