For some reason when the shell_exec runs, then I run the while loop and it will remove files it will not wait until all files are removed. I even added a sleep to slow it down. The issue is by the time it reaches the update statement (where the line says: "// files should be gone by now -- >" ) the files should fully be deleted and its not.
shell_exec("rm -r ".$_SESSION['todays_backup_dir']);
$rm_count = 0;
$dir_exist=1;
while( ($dir_exist==1) || ( $rm_count <= 20) )
{
echo "Still deleting files, sleeping for 5 seconds <br>";
$fh = fopen($_SESSION['log_file'], 'a');
fwrite($fh, "Still deleting files, sleeping for 10 seconds \n");
fclose($fh);
sleep(10);
if(file_exists($_SESSION['todays_backup_dir']))
{
$dir_exist=1;
$fh = fopen($_SESSION['log_file'], 'a');
fwrite($fh, "Still deleting files, sleeping for 10 seconds \n");
fclose($fh);
sleep(10);
}
else
{
$dir_exist=0;
echo "Directory was removed successfully <br>";
$fh = fopen($_SESSION['log_file'], 'a');
fwrite($fh, "Directory was removed successfully \n");
fwrite($fh, "Sleeping 5 seconds then exiting loop \n");
sleep(5);
fwrite($fh, "updating watcher settings set currently_watch = 'Y' \n");
// files should be gone by now, update table -- >
$upd_monitor_on = mysql_query("update watcher_settings set currently_watch = 'Y'") or die(mysql_error());
fclose($fh);
break;
}
$rm_count = $rm_count + 1;
}
if($dir_exist==1)
{
sleep(10);
$fh = fopen($_SESSION['log_file'], 'a');
fwrite($fh, "Directory was not removed, dont want to leave the monitor off, setting back to on as the default setting \n");
fwrite($fh, "updating watcher settings set currently_watch = 'Y' \n");
fclose($fh);
// even though the backup directory was not removed we don't want the monitor to remain off
$upd_monitor_on = mysql_query("update watcher_settings set currently_watch = 'Y'") or die(mysql_error());
}