oh_php_designer Posted April 7, 2015 Share Posted April 7, 2015 (edited) 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()); } Edited April 7, 2015 by oh_php_designer Quote Link to comment https://forums.phpfreaks.com/topic/295340-code-not-waiting-for-files-to-remove/ Share on other sites More sharing options...
requinix Posted April 7, 2015 Share Posted April 7, 2015 The result from functions like file_exists() are cached for the duration of your script, if not longer. When the shell_exec() ends the files should be fully deleted. Use clearstatcache to clear the cache before you verify that. Quote Link to comment https://forums.phpfreaks.com/topic/295340-code-not-waiting-for-files-to-remove/#findComment-1508458 Share on other sites More sharing options...
oh_php_designer Posted April 8, 2015 Author Share Posted April 8, 2015 So I added clearstatcache(); Right after the shell_exec command and it didn't help.. Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/295340-code-not-waiting-for-files-to-remove/#findComment-1508495 Share on other sites More sharing options...
IThinkMyBrainHurts Posted April 8, 2015 Share Posted April 8, 2015 I read this one yesterday and what I took from requinix's link was that unlink() was the thing to use. The use of clearstatcache() to me / the manual, doesn't affect your code since you are aren't using PHP to remove the files, rather the systems shell. I am surprised that the command doesn't finish before returning. On reading the manual about shell_exec() there are no mentions of it not completing the operation before returning. The only thing I can see which may affect that is you not getting the response from shell_exec(), so maybe (just maybe) changing your first line to: $output=shell_exec("rm -r ".$_SESSION['todays_backup_dir']); But still not sure that'll do it. Personally I'd write my own recursive function in PHP and use unlink(), then you'll be sure... Also not sure if Windows systems have the "rm" command (rather RMDIR, DEL, ERASE, DELTREE), so this method would be platform independent too. Quote Link to comment https://forums.phpfreaks.com/topic/295340-code-not-waiting-for-files-to-remove/#findComment-1508498 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.