【发布时间】:2020-08-29 14:03:34
【问题描述】:
我有以下脚本可以运行以从文件中读取新内容:
<?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 更改文件,我会在 Windows 中看到文件更改为 filesize 和内容,但由于某种原因,Apache 需要大约 4-6 秒才能检测到更改。在这 4-6 秒内,它会收到更改前的旧 filesize。
所以我有以下问题:
-
filesize信息是否缓存在 Apache 服务器或 Windows 内部的任何位置? - 如果问题 1 适用,是否可以删除或禁用此缓存?
- 这可能不是缓存问题吗?
【问题讨论】:
-
php.net/manual/en/function.filesize.php 阅读注意部分:
Note: The results of this function are cached. See clearstatcache() for more details. -
我认为在您的本地开发环境中 php.ini 的
realpath_cache_ttl参数较低。 -
@num8er 我已经在使用
clearstatcache(),正如您在我的脚本中看到的那样。问题比这更复杂,因为它涉及 UNC 位置。clearstatcache()也主要(如果不是总是)用于调用脚本内的同一文件。我的问题在于脚本的每次调用。不过谢谢你的建议。
标签: php apache file-get-contents filesize