Skip to content

Instantly share code, notes, and snippets.

@pricejn2
Created January 12, 2014 02:40
Show Gist options
  • Save pricejn2/8380051 to your computer and use it in GitHub Desktop.
Save pricejn2/8380051 to your computer and use it in GitHub Desktop.
script execution from php with timeout
// convert
$timeout = 30;
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w"), // stderr
);
$process = proc_open($youtdl, $descriptorspec, $pipes);
var_dump($pipes);
if (isset($stdin)) {
fwrite($pipes[0],$stdin);
}
fclose($pipes[0]);
stream_set_timeout($pipes[1], 0);
stream_set_timeout($pipes[2], 0);
$stdout = '';
$start = microtime();
while ($data = fread($pipes[1], 4096)) {
$meta = stream_get_meta_data($pipes[1]);
if (microtime()-$start>$timeout) break;
if ($meta['timed_out']) continue;
$stdout .= $data;
}
$stdout .= stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
$return = proc_close($process);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment