title: weird PHP filesize behavior
body: <p>In the code below I...</p>
  
  <ol>
  <li><p>open a textfile, write four characters to it, and close it again,</p></li>
  <li><p>re-open it, read the contents, and use filesize to report the size of the file. (It's 4, as it should be),</p></li>
  <li><p>manipulate those contents and tack on four more characters as well. Then I write that new string to the textfile and close it again,</p></li>
  <li><p>use filesize again to report how big the file is.</p></li>
  </ol>
  
  <p>To my surprise, the answer it gives is still 4 even though the actual size of the file is 8! Examining the contents of the file proves that the write works and the length of the contents is 8.</p>
  
  <p>What is going on??</p>
  
  <p>By the way I have to use fread and fwrite instead of file_get_contents and file_put_contents. At least I think I do. This little program is a stepping stone to using "flock" so I can read the contents of a file and rewrite to it while making sure no other processes use the file in between. And AFAIK flock doesn't work with file_get_contents and file_put_contents.</p>
  
  <p>Please help!</p>
  
  <pre><code>&lt;?php
  $filename = "blahdeeblah.txt";
  
  // Write 4 characters
  $fp = fopen($filename, "w");
  fwrite($fp, "1234");
  fclose($fp);
  
  // read those characters, manipulate them, and write them back (also increasing filesize).
  $fp = fopen($filename, "r+");
  $size = filesize($filename);
  echo "size before is: " . $size . "&lt;br&gt;";
  $t = fread($fp, $size);
  $t = $t[3] . $t[2] . $t[1] . $t[0] . "5678";
  rewind($fp);
  fwrite($fp, $t);
  fclose($fp);
  
  // "filesize" returns the same number as before even though the file is larger now.
  $size = filesize($filename);
  echo "size after is: " . $size . "   ";
  
  ?&gt;
  </code></pre>