PHP can't unlink file after fclose
<p>After my script finishes I can delete the file, but while it's running I can't touch it, even after an <code>fclose()</code>. Here is the code I'm trying to use:</p>

<pre><code>    $Files = glob("$_SERVER[DOCUMENT_ROOT]/files/*.csv");

    $File = fopen($Files[0], "r");        

    //while (($Data = fgetcsv($File)) !== false) {...

    $i = 0;
    while (is_resource($File)) {
        $i &amp;&amp; sleep(1);
        echo "Closing handle.\n";
        fclose($File);
        if (++$i &gt; 5)
            die("Could not close handle.");
    }

foreach ($Files as $File) {
    $i = 0;
    while (file_exists($File)) {
        $i &amp;&amp; sleep(1);
        echo "Deleting file.\n";
        @unlink($File);
        echo 'www-data@debian:~$ ', $Command = "rm -f '$File' 2&gt;&amp;1", "\n", shell_exec($Command);
        if (++$i &gt; 5)
            die("Could not delete the file.");
    }
}
</code></pre>

<p>As you can see I'm trying to delete it through <code>unlink()</code> and using shell commands and neither are working, both give me this error:</p>

<blockquote>
  <p>rm: cannot remove
  'invincible-file.csv':
  Text file busy</p>
</blockquote>

<p>I also realize the script might be a bit overkill, but that's purely because I couldn't get it to work in any way, and some of the extra code is just for output purposes to try and debug what is happening. </p>