Jump to content

Recommended Posts

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 by oh_php_designer
Link to comment
https://forums.phpfreaks.com/topic/295340-code-not-waiting-for-files-to-remove/
Share on other sites

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.