Jump to content

Shell Zip Complete or Error?


MoFish
Go to solution Solved by kicken,

Recommended Posts

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

Link to comment
Share on other sites

  • Solution

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.
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.