Skip to content

Instantly share code, notes, and snippets.

@chamberlainpi
Created June 10, 2016 03:53
Show Gist options
  • Save chamberlainpi/09adbcf18b25ced9449d8fd2bdc8e583 to your computer and use it in GitHub Desktop.
Save chamberlainpi/09adbcf18b25ced9449d8fd2bdc8e583 to your computer and use it in GitHub Desktop.
PHP proc_open random refresh issue
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CMS</title>
</head>
<body>
<a href='?node=npm&node_args=list'>Click to run "npm list"</a><br/>
<pre>
<?php
if(!isset($_GET['node'])) die();
function trace($msg) { echo "$msg\n"; }
$errorFile = "./.node-errors.txt";
$descriptorspec = [
0 => ["pipe", "r"], // stdin is a pipe that the child will read from
1 => ["pipe", "w"], // stdout is a pipe that the child will write to
2 => ["file", $errorFile, "a"] // stderr is a file to write to
];
$nodeCommand = $_GET['node'];;
if(isset($_GET['node_args'])) {
trace("NodeJS Args: " . $_GET['node_args'] . "\n\n");
$nodeArgs = $_GET['node_args'];
} else {
$nodeArgs = '';
}
//??? test
//$nodeCommand $nodeArgs
$process = proc_open("npm list", $descriptorspec, $pipes, getcwd());
if (is_resource($process)) {
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);
fclose($pipes[0]);
//fclose()
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);
if ($return_value !== 0) {
trace("ERROR: command returned $return_value");
echo file_get_contents($errorFile);
//file_put_contents($errorFile, ''); //Clear the error
} else {
trace("===============================\nCompleted server-side command.");
}
}
?>
</pre>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment