Search code examples
phpcachingfopenfgetcsv

Reading an URL with fopen + fgetcsv: does it use a cache?


I have a CSV online which is sometimes updated. I'm opening and reading it with:

$f = fopen("http://www.example.com/myfile.csv", "r"); 
$headers = fgetcsv($f); 
while ($row = fgetcsv($f)) 
{
    echo $row[0];
}
fclose($f);

Sometimes I notice the following problem: I know that the online CSV is already updated, but it seems that this PHP code reads an older version of the CSV (that was read previously a few seconds/minutes before by a precedent call of the same PHP code), like if it would use a cached version of myfile.csv.

Where is the cache setting for fopen($url, "r")? Is there a way to force re-download the URL and bypass any cache system?

PS: Internally, what does fopen use for opening non-filesystem-files but distant files/internet URLs?


Solution

  • In fact fopen does not seem to be affected by caching, the documentation https://www.php.net/manual/en/function.clearstatcache.php states:

    Affected functions include stat(), lstat(), file_exists(), is_writable(), is_readable(), is_executable(), is_file(), is_dir(), is_link(), filectime(), fileatime(), filemtime(), fileinode(), filegroup(), fileowner(), filesize(), filetype(), and fileperms()