Skip to content

Instantly share code, notes, and snippets.

@dividy
Created September 7, 2021 07:06
Show Gist options
  • Save dividy/2f3c4f88df74e7c7157694655b91c930 to your computer and use it in GitHub Desktop.
Save dividy/2f3c4f88df74e7c7157694655b91c930 to your computer and use it in GitHub Desktop.
<?php
function liveExecuteCommand($cmd)
{
while (@ ob_end_flush()); // end all output buffers if any
$proc = popen("$cmd 2>&1 ; echo Exit status : $?", 'r');
$live_output = "";
$complete_output = "";
while (!feof($proc))
{
$live_output = fread($proc, 4096);
$complete_output = $complete_output . $live_output;
echo str_replace("\n","<br>", $live_output);
@ flush();
}
pclose($proc);
// get exit status
preg_match('/[0-9]+$/', $complete_output, $matches);
// return exit status and intended output
return array (
'exit_status' => intval($matches[0]),
'output' => str_replace("Exit status : " . $matches[0], '', $complete_output)
);
}
echo "<code>";
$command='zip -r ../html.zip ./';
$result = liveExecuteCommand($command);
echo "</code>";
echo "<hr>";
if($result['exit_status'] === 0){
echo "execution OK.";
} else {
echo "execution FAILED!";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment