php how to lock a file only for read when it is writing?
<p>I have a page with a larger query form mysql, the results could be more than 2MB. Because I haven't got enough memory on my server (I should arrange the memory for mysql to speed up query time), I have given up memcached. instead it is a txt file.</p>

<p>How to lock a file like this situation? If the file is lock, only read, if file isn't lock, first write then read. thanks.</p>

<pre><code>if((time()-filemtime(filename)) &gt; 60){
   //mysql_query;  query time less than 1.5 seconds
   if(ex_lock){ // if file is lock, only read
      file_get_contents($filename);
   }else{  // if file isn't lock, first write then read
      file_put_contents($filename, $query_contents);//some file size could be 2mb
      file_get_contents($filename);
   }
}
</code></pre>
