我正在尝试将记录保存到文件中,当文件大于10MB时,应创建新文件。 (这个过程同时发生。这就是我使用文件锁的原因)
但是,文件变为100KB时变为空。 (它不会创建任何新文件,但会启动一个具有相同名称的空文件。)
我的代码有什么问题?锁定文件上的file_get_contents
会返回null
吗?
任何建议或意见将不胜感激。
提前谢谢。
$archiveSaveContents = [];
$archiveSaveContents = 'some record';
$archiveFileExtension = 'json';
$archiveFilePath = FCPATH.'atch/driver_archive_location/'.$d->format('Y-m-d').'/'.$reqDate->format('H').'/';
$archiveFiles = scandir($archiveFilePath);
$archiveFiles = array_diff(scandir($archiveFilePath), array('.', '..'));
$archiveFilePostfix = ( count($archiveFiles) <= 1 ) ? 1 : count($archiveFiles);
$archiveFileName = DEVELOP_HOST.'_'.'driversLocation_'.$archiveFilePostfix.'.'.$archiveFileExtension;
$archiveFullPath = $archiveFilePath.$archiveFileName;
$archiveLimitSize = 10;
$archiveFileSize = ( filesize($archiveFullPath ) * .0009765625) * .0009765625;
if ( $archiveFileSize > $archiveLimitSize ) {
$archiveFilePostfix = ( $archiveFilePostfix + 1 );
$archiveFileName = DEVELOP_HOST.'_'.'driversLocation_'.$archiveFilePostfix.'.'.$archiveFileExtension;
$archiveFullPath = $archiveFilePath.$archiveFileName;
}
$archiveFile = json_decode(file_get_contents($archiveFullPath),true);
$archiveFile[] = $archiveSaveContents;
if ( !is_dir($archiveFilePath) ) {
@mkdir($archiveFilePath, 0777, TRUE);
@chmod($archiveFilePath, 0777);
}
// if ( !$fp = @fopen($archiveFullPath, FOPEN_WRITE_CREATE) ) {
if ( !$fp = @fopen($archiveFullPath, 'w+') ) {
if ( false === $fp ) {
throw new RuntimeException('Unable to open log file for writing');
}
return FALSE;
}
flock($fp, LOCK_EX);
$write = fwrite($fp,json_encode($archiveFile));
flock($fp, LOCK_UN);
fclose($fp);
@chmod($archiveFullPath, FILE_WRITE_MODE);
编辑代码:我怀疑文件不安全锁定。所以我在文件上使用了安全锁。但它仍然给我同样的错误。然后我发现在写文件的过程中,文件中json形式的字符串在写入时会被破坏。
$archiveFileName = DEVELOP_HOST.'_'.'driversLocation_'.$archiveFilePostfix.'.'.$archiveFileExtension;
$archiveFullPath = $archiveFilePath.$archiveFileName;
if ( !is_dir($archiveFilePath) ) {
@mkdir($archiveFilePath, 0777, TRUE);
@chmod($archiveFilePath, 0777);
}
$archiveLimitSize = 10;
$archiveFileSize = ( filesize($archiveFullPath ) * .0009765625) * .0009765625;
if ( $archiveFileSize > $archiveLimitSize ) {
$archiveFilePostfix = ( $archiveFilePostfix + 1 );
$archiveFileName = DEVELOP_HOST.'_'.'driversLocation_'.$archiveFilePostfix.'.'.$archiveFileExtension;
$archiveFullPath = $archiveFilePath.$archiveFileName;
}
$readFp = fopen($archiveFullPath, 'r');
if ( $readFp ) {
while (!flock($readFp, LOCK_SH)) { usleep(1); }
$contents = fread($readFp, filesize($archiveFullPath));
flock($readFp, LOCK_UN);
fclose($readFp);
if ( !$fp = @fopen($archiveFullPath, 'r+') ) {
if ( false === $fp ) {
throw new RuntimeException('Unable to open log file for writing');
}
return FALSE;
}
}
else {
if ( !$fp = @fopen($archiveFullPath, 'w+') ) {
if ( false === $fp ) {
throw new RuntimeException('Unable to open log file for writing');
}
return FALSE;
}
}
$archiveFile = json_decode($contents);
$archiveFile[] = $archiveSaveContents;
$pieces = str_split(json_encode($archiveFile), 1024 * 4);
while (!flock($fp, LOCK_EX)) { usleep(1); }
foreach ( $pieces as $piece ) {
fwrite($fp, $piece, strlen($piece));
}
fflush($fp);
flock($fp, LOCK_UN);
fclose($fp);
@chmod($archiveFullPath, FILE_WRITE_MODE);