Check if a file is writable before writing to it
<p>I'm trying to create a script which will check if a file is writable before writing to it,</p>

<p>Making sure the script doesn't exit prematurely.</p>

<p>I've gotten this far</p>

<pre><code>$meta =stream_get_meta_data($file);
while(!is_writable($meta['uri'])){
  sleep(rand(0,3));
  $meta=stream_get_meta_data($file);
  echo("sleeping\n");
}
$csv = fopen($file, 'a+')or die("can't open file");
</code></pre>

<p>When I test the script with $file open, it blocks on the sleeping part even after $file is closed.</p>

<p>I'm fairly new to PHP, so there might be a processing paradigm that i'm not aware of.</p>

<p>Any help would be very welcome.</p>

<p>EDIT : The reason I entered this into a while loop is to continually check if the file is open or not. Hence it should only exit the while loop once the file is finally writable.</p>

<p>The sleep is simply to replicate a person trying to open the file.</p>
