我有一个我写的文件
<timestamp>hashedcertificate
<timestamp>hashedcertificate
<timestamp>hashedcertificate
(等)
在某个请求中,我正在读出数组中的所有时间戳和数组中的所有哈希字符串。
$valid = fopen("./valid", "r+");
if (!$valid) {
log::Write("Could not open file. Exiting..", DEBUG);
exit(1);
}
$isLocked = flock($valid, LOCK_EX);
while (!$isLocked)
$isLocked = flock($valid, LOCK_EX);
while (!feof($valid)) {
$pos_begin = strpos($line, "<");
$pos_end = strpos($line, ">", $pos_begin);
$timestamp = substr($line, $pos_begin+1, $pos_end - $pos_begin - 1);
$timestamps[] = $timestamp;
$storedCert = substr($line, $pos_end + 1);
$storedCerts[] = $storedCert;
log::Write(sprintf("Read: %s with timestamp %s", $storedCert, $timestamp), DEBUG);
$line = fgets($valid);
}
检查后(如果时间戳太旧,则删除),我想将剩余的有效哈希值写回文件。
ftruncate($valid, 0);
$counter = 0;
foreach ($timestamps as $timestamp) {
$toWrite = "<" . $timestamp . ">" . $storedCerts[$counter] . "\n";
log::Write(sprintf("Writing: \n%s", $toWrite), DEBUG);
fputs($valid, $toWrite);
$counter += 1;
}
flock($valid, LOCK_UN);
fclose($valid);
但是我的文件看起来总是这样:(在我认为ftruncate之后,因为当我打开文件附加时间戳+哈希时,它会没有奇怪的东西;只有从文件中读取+删除数组元素+ ftruncate +写回文件的东西来了) (请忽略第一个哈希;我暂时将其更改为crc32以使其更短):
^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @&LT ; 1378903136&GT; 9f71fb266d96afa161c1e52e8b65031c08997bdb5f215f7d
<1378903666>b0e15296
<1378903671>6b4132b9
<1378903695>b0e15296
我做错了什么?
答案 0 :(得分:2)
截断之后你不应该做一个倒带()来重新指向文件开头的指针吗?