PHP cannot disable file after fclose

After the script ends, I can delete the file, but while it works, I can not touch it even after fclose() . Here is the code I'm trying to use:

  $Files = glob("$_SERVER[DOCUMENT_ROOT]/files/*.csv"); $File = fopen($Files[0], "r"); //while (($Data = fgetcsv($File)) !== false) {... $i = 0; while (is_resource($File)) { $i && sleep(1); echo "Closing handle.\n"; fclose($File); if (++$i > 5) die("Could not close handle."); } foreach ($Files as $File) { $i = 0; while (file_exists($File)) { $i && sleep(1); echo "Deleting file.\n"; @unlink($File); echo 'www-data@debian:~$ ', $Command = "rm -f '$File' 2>&1", "\n", shell_exec($Command); if (++$i > 5) die("Could not delete the file."); } } 

As you can see, I am trying to remove it using unlink() and using shell commands, and none of them work, both give me this error:

rm: cannot delete 'Invincible-file.csv': Text file busy

I also understand that the script may be a little redundant, but this is because I could not get it to work in any way, and some of the additional code is only for output, to try and debug what is happening,

+8
php rm fopen fclose unlink
source share
1 answer

I ran into this problem earlier and was able to fix it by forcing garbage collection after closing the file and before disconnecting it:

 gc_collect_cycles(); 

This is actually not the best solution, but it solved the problem that I had previously deleted previously opened and closed rows.

+5
source share

All Articles