MoFish Posted August 25, 2016 Share Posted August 25, 2016 Hi, I am running a zip command via exec which zips my website up. This works well, however I am a little unsure on the best way to check if this was successfull or not. Should I simply check if the file exists or is there a better way to do this? if(isset($_POST['action'])) { switch ($_POST['action']) { case "zip": $script = "cd /websites/N2eGK46OjC/; zip -r -o /call/remote_skeleton.zip *"; $var = exec($script, $output, $return_var); return $return_var; // this returns 0 by default break; } } Ideally i'm wanting to run this from another file and return the response, however do not know how to best return success or error. <script> $(document).ready(function(){ $("#btnGo").click(function() { $.ajax({url: 'action.php', data: {action : 'zip'}, type: 'post', async: 'true', dataType: 'json', beforeSend: function() { $("#loading").show(); // This will show ajax spinner }, complete: function() { $("#loading").hide(); // This will hide ajax spinner }, success: function (result) { // not sure how to capture here?? }, error: function (request,error) { // not sure how to capture here?? alert('error'); } }); }); }); </script> Any helps much appriciated. Cheers, MoFish Quote Link to comment https://forums.phpfreaks.com/topic/301996-shell-zip-complete-or-error/ Share on other sites More sharing options...
Solution kicken Posted August 25, 2016 Solution Share Posted August 25, 2016 You need to check the return value of the command you executed, which is provided to you through the third parameter to exec ($return_var). The manual page for zip lists a bunch of possible return codes, but the one you're interested in is: 0 normal; no errors or warnings detected. So you'd have something like this: exec('cd /websites/N2eGK46OjC/; zip -r -o /call/remote_skeleton.zip *', $output, $return); if ($return !== 0){ //Error encountered trigger_error("Error running command", E_USER_ERROR); } else { //Success } Once you know if it failed or was successful you could pass back some JSON to your javascript using json_encode containing whatever data you need. Quote Link to comment https://forums.phpfreaks.com/topic/301996-shell-zip-complete-or-error/#findComment-1536613 Share on other sites More sharing options...
Jacques1 Posted August 25, 2016 Share Posted August 25, 2016 I am running a zip command via exec which zips my website up. And why aren't you using PHP for that? Quote Link to comment https://forums.phpfreaks.com/topic/301996-shell-zip-complete-or-error/#findComment-1536614 Share on other sites More sharing options...
MoFish Posted August 26, 2016 Author Share Posted August 26, 2016 Hi Jacques, Thanks for that - worked perfect. I was using PHP before, however found that it exceeded my memory_limits on my server everytime it was zipping. The exec method seemed to be the only way it would zip without falling over. Cheers, MoFish Quote Link to comment https://forums.phpfreaks.com/topic/301996-shell-zip-complete-or-error/#findComment-1536679 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.