使用gzwrite()和gzclose()后,filesize不正确

The code itself is quite large so here is the boiled down version:

<?php
//set common headers
ob_start();
//generate the javascript source
$ret=ob_get_clean();
$handle=gzopen('cache/file.js.gz','wb');
gzwrite($handle,$ret);
gzclose($handle);
header('Content-Length: '.filesize('cache/file.js.gz'),true);
readfile('cache/file.js.gz');

The fun part. If cache/file.js.gz doesn't exist readfile('cache/file.js.gz'); reports the correct size. If it does exists then it reports the old size of the file.

I have tried adding fflush($handle) after the gzwrite with the same result. I have not tried unlinking the file before writing. Seems like a superfluous step.

The output files are generally pretty small. The largest being 5kB.

Server Info:

  • Apache version 2.2.23
  • PHP version 5.2.17

call clearstatcache() before getting file stats