ankur0101 Posted January 5, 2012 Share Posted January 5, 2012 Hello, I am making a full backup of cPanel account. It takes few seconds for zipping 4 to 10 MB of data but when there are more than 200 MB of data on cpanel account, obviously it will take some more time, say for example 2 minutes. On cpanel based servers, while zipping, it temporarily creates a ZIP file of 33KB And this file's size stays at 33KB until the process get completed i.e. till it completes a backup file So conditionCHK is >> If example.zip > 33KB then, backup ZIP file is ready else sleep(15) and check conditionCHK again Point is that, it should check condition again and again until it satisfy. So in my case, it script will check again and again whether example.zip has become more than 33KB or not. If it not, then sleep for 15 seconds and again check Once condition will satisfy, it will show "Success" Which loop can I use here ? I am totally confused in developing a logic. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/254413-how-to-check-condition-again-and-again-until-it-satisfy/ Share on other sites More sharing options...
Nodral Posted January 5, 2012 Share Posted January 5, 2012 I'm no expert on this, but a recursive function would do the task you need. ie function conditionCHK(){ if (example.zip > 33 ){ $result= "done"; } else { sleep(15); conditionCHK(); } return $result; } As I said, I'm no expert so you will need to check the syntax and maybe read up on recursive function, but I's say this is probably your best bet. Quote Link to comment https://forums.phpfreaks.com/topic/254413-how-to-check-condition-again-and-again-until-it-satisfy/#findComment-1304482 Share on other sites More sharing options...
ankur0101 Posted January 5, 2012 Author Share Posted January 5, 2012 Hi, Can I do without creating a function ? Quote Link to comment https://forums.phpfreaks.com/topic/254413-how-to-check-condition-again-and-again-until-it-satisfy/#findComment-1304484 Share on other sites More sharing options...
Nodral Posted January 5, 2012 Share Posted January 5, 2012 Why do you not want to create a function? You're going to be performing the same routine possibly hundreds of times depending on your processing speed. This is one of the main reasons for using functions. You could (theoretically) nest hundreds of If/else statements to acheive the same thing, but why bother when you can reuse the same code you write once? Quote Link to comment https://forums.phpfreaks.com/topic/254413-how-to-check-condition-again-and-again-until-it-satisfy/#findComment-1304487 Share on other sites More sharing options...
ankur0101 Posted January 5, 2012 Author Share Posted January 5, 2012 I am doing modifications with my code, I will get back here. Quote Link to comment https://forums.phpfreaks.com/topic/254413-how-to-check-condition-again-and-again-until-it-satisfy/#findComment-1304489 Share on other sites More sharing options...
ankur0101 Posted January 5, 2012 Author Share Posted January 5, 2012 I did so, But script has went into loop which will never stop. From 5 minutes, it is loading, loading and loading. Now I am removing sleep(15) I will get back here Quote Link to comment https://forums.phpfreaks.com/topic/254413-how-to-check-condition-again-and-again-until-it-satisfy/#findComment-1304493 Share on other sites More sharing options...
Nodral Posted January 5, 2012 Share Posted January 5, 2012 The loop will go forever until the if condition is satisfied. You may have an issue with your if statement. Post your function code and we'll have a look see Quote Link to comment https://forums.phpfreaks.com/topic/254413-how-to-check-condition-again-and-again-until-it-satisfy/#findComment-1304495 Share on other sites More sharing options...
ankur0101 Posted January 5, 2012 Author Share Posted January 5, 2012 The whole script is loading again and again even that part which is out of function, when I try to execute script again, it gives me 500 Server error Quote Link to comment https://forums.phpfreaks.com/topic/254413-how-to-check-condition-again-and-again-until-it-satisfy/#findComment-1304497 Share on other sites More sharing options...
Nodral Posted January 5, 2012 Share Posted January 5, 2012 Have you got error_reporting switched on? Nobody will be able to help you much more unless we can see your code and an error Quote Link to comment https://forums.phpfreaks.com/topic/254413-how-to-check-condition-again-and-again-until-it-satisfy/#findComment-1304498 Share on other sites More sharing options...
Philip Posted January 5, 2012 Share Posted January 5, 2012 Sounds like this is a job for the while loop <?php // somewhat psuedo-code $i = 0; while(filesize('file.zip') == 33kb) { // Add a timeout, otherwise you'll end up in an endless loop if($i > 1000) break; $i++; // sleeeep! sleep(15); } Quote Link to comment https://forums.phpfreaks.com/topic/254413-how-to-check-condition-again-and-again-until-it-satisfy/#findComment-1304525 Share on other sites More sharing options...
Nodral Posted January 5, 2012 Share Posted January 5, 2012 Using a while loop is ok, except if the processing speed is incredibly slow for some reason, $i will hit 1000 and the loop will stop without the procedure completing. The correct way to do this is with a recursive function which will go forever until the conditions are met. As I said earlier, the problem sounds as though it s with the code in the script, not the functionality of using a recursive function. Until ankur0101 posts some code and/or errors we're all using guesswork as to how to help him. Quote Link to comment https://forums.phpfreaks.com/topic/254413-how-to-check-condition-again-and-again-until-it-satisfy/#findComment-1304526 Share on other sites More sharing options...
Philip Posted January 5, 2012 Share Posted January 5, 2012 Using a while loop is ok, except if the processing speed is incredibly slow for some reason, $i will hit 1000 and the loop will stop without the procedure completing. The correct way to do this is with a recursive function which will go forever until the conditions are met. You can easily remove the failsafe, but I would not really recommend it. The reason for adding it is so that you don't have 5 scripts that will never end because there was an error on the cpanel processing side for whatever reason. The failsafe does not just have to break out of the loop and that's that, it could also call an error logger or something along those lines. Quote Link to comment https://forums.phpfreaks.com/topic/254413-how-to-check-condition-again-and-again-until-it-satisfy/#findComment-1304529 Share on other sites More sharing options...
kicken Posted January 5, 2012 Share Posted January 5, 2012 The correct way to do this is with a recursive function which will go forever until the conditions are met. And if the script takes a long time to process (or errors) your recursive function is going to annihilate your stack by making it grow too large, thus causing php to crash. No, the correct way to handle this is with a simple loop. Also, given the sleep(15) you'll be waiting 4 hours before you hit the $i > 1000 failsafe. Quote Link to comment https://forums.phpfreaks.com/topic/254413-how-to-check-condition-again-and-again-until-it-satisfy/#findComment-1304534 Share on other sites More sharing options...
ankur0101 Posted January 7, 2012 Author Share Posted January 7, 2012 Sounds like this is a job for the while loop <?php // somewhat psuedo-code $i = 0; while(filesize('file.zip') == 33kb) { // Add a timeout, otherwise you'll end up in an endless loop if($i > 1000) break; $i++; // sleeeep! sleep(15); } This thought came in my mind, we are limiting attempts by counter. This is not exactly what I want but this will work Thanks Quote Link to comment https://forums.phpfreaks.com/topic/254413-how-to-check-condition-again-and-again-until-it-satisfy/#findComment-1305328 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.