After my script finishes I can delete the file, but while it's running I can't touch it, even after an 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'm trying to delete it through unlink()
and using shell commands and neither are working, both give me this error:
rm: cannot remove 'invincible-file.csv': Text file busy
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.
$Files
? Also, you usefopen
with the "r" mode for reading and you are trying to delete the file later. Are you sure that the user has permission to delete? Maybe it's a permission issue. Try doing theunlink
without any of the other code. – Malignity@
before theunlink
so you can let php tell you why it failed. Also, check the return value offclose
. – Jesusunlink
without thefopen
at all, and I've added even more code to the example, hopefully I'm not missing any more important parts – Dermatosisfopen
, etc. it deletes all of the files without any issues. It's only once I open and close the file withing the PHP script that I can no longer delete it – Dermatosisinvincible-file.csv
resides? – Squarelyfclose
? It should be true. – Malignity