Added proc_open for execute commands in php

This commit is contained in:
absurdo 2023-11-27 13:05:13 +01:00
parent 8ae64c74d2
commit 995208b3e1

View file

@ -7,7 +7,66 @@ class Linux {
static public function shell_command($arr_command, $exit_if_error=1) {
$cmd=implode(' ', $arr_command);
$error=0;
for($x=1;$x<count($arr_command);$x++) {
$arr_command[$x]=escapeshellarg($arr_command[$x]);
}
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w") // stderr
);
$pipes=[];
$process=proc_open($final_command, $descriptorspec, $pipes);
if(is_resource($process)) {
while($s=fgets($pipes[1])) {
echo $s;
flush();
}
while($e=fgets($pipes[2])) {
echo $e;
flush();
}
}
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
$result=proc_close($process);
if($result>0) {
echo 'Error executing command '.$cmd."\n";
if($exit_if_error) {
exit($result);
}
else {
return $result;
}
}
/*$cmd=implode(' ', $arr_command);
$a = popen($cmd, 'r');
@ -19,8 +78,6 @@ class Linux {
}
flush();
$result=pclose($a);
if($result>0) {
@ -38,7 +95,7 @@ class Linux {
}
}
}*/
}