我有以下脚本,用于从文件中读取新内容:
<?php
clearstatcache();
$fileURL = "\\\\saturn\extern\seq_ws.csv";
$fileAvailable = file_exists($fileURL);
$bytesRead = file_get_contents("bytes.txt");
if($fileAvailable){
$fileSize = filesize($fileURL);
//Statusses 1 = Partial read, 2 = Complete read, 0 = No read, -1 File not found. followed by !!
if($bytesRead < $fileSize){
//$bytesRead till $fileSize bytes read from file.
$content = file_get_contents($fileURL, NULL, NULL, $bytesRead);
file_put_contents("bytes.txt", ((int)$bytesRead + strlen($content)));
echo "1!!$content";
}else if($bytesRead > $fileSize){
//File edit or delete detected, whole file read again.
$content = file_get_contents($fileURL);
file_put_contents("bytes.txt", strlen($content));
echo "2!!$content";
}else if($bytesRead == $fileSize){
//No new data found, no action taken.
echo "0!!";
}
}else{
//File delete detected, reading whole file when available
echo "-1!!";
file_put_contents("bytes.txt", "0");
}
?>
当我运行它并做我期望的事情时,它的工作非常完美。当我从同一台PC和我的服务器编辑文件时,它会立即工作并返回正确的值。但是,当我从另一台PC编辑文件时,我的脚本大约需要4-6秒才能读取文件的正确文件大小。
我在脚本的基础上添加了clearstatcache();
,因为我认为这是一个缓存问题。但奇怪的是,当我从服务器PC上更改文件时,它会立即响应,但从另一个服务器上却不会响应。
最重要的是,一旦另一台PC改变了文件,我就会看到filesize
和内容在Windows中的文件变化,但是出于某种原因,Apache需要大约4-6秒的时间来检测更改。在这4-6秒内,它接收更改之前的旧filesize
。
因此,我有以下问题:
filesize
信息是否缓存在任何地方,可能是在Apache服务器上,还是在Windows中?发布于 2020-05-13 17:05:58
我认为在您的本地PC php中有开发设置。
因此,我建议检查php.ini中的这个帕拉姆:realpath_cache_ttl
即:
realpath_cache_ttl integer
为给定文件或目录缓存realpath信息的时间(以秒为单位)。对于很少更改文件的系统,请考虑增加该值。
为了测试它,php在本地和服务器上都要检查这个值:
<?php phpinfo();
https://stackoverflow.com/questions/61773609
复制相似问题