note 73750 added to function.proc-open

From: Date: Thu, 08 Mar 2007 20:30:29 +0000
Subject: note 73750 added to function.proc-open
Groups: php.notes 
Request: Send a blank email to php-notes+get-124246@lists.php.net to get a copy of this message
If you just want to execute a command and get the output of the program, here is a simple object-oriented way to do it: If there was an error detected from the STDERR output of the program, the open method of the Process class will throw an Exception. Otherwise, it will return the STDOUT output of the program. <?php class Process { public static function open($command) { $retval = ''; $error = ''; $descriptorspec = array( 0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'r') ); $resource = proc_open($command, $descriptorspec, $pipes, null, $_ENV); if (is_resource($resource)) { $stdin = $pipes[0]; $stdout = $pipes[1]; $stderr = $pipes[2]; while (! feof($stdout)) { $retval .= fgets($stdout); } while (! feof($stderr)) { $error .= fgets($stderr); } fclose($stdin); fclose($stdout); fclose($stderr); $exit_code = proc_close($resource); } if (! empty($error)) throw new Exception($error); else return $retval; } } try { $output = Process::open('cat example.txt'); // do something with the output } catch (Exception $e) { echo $e->getMessage() . "\n"; // there was a problem executing the command } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 69.2.234.66 ---- Manual Page -- http://www.php.net/manual/en/function.proc-open.php Edit -- https://master.php.net/note/edit/73750 Del: integrated -- https://master.php.net/note/delete/73750/integrated Del: useless -- https://master.php.net/note/delete/73750/useless Del: bad code -- https://master.php.net/note/delete/73750/bad+code Del: spam -- https://master.php.net/note/delete/73750/spam Del: non-english -- https://master.php.net/note/delete/73750/non-english Del: in docs -- https://master.php.net/note/delete/73750/in+docs Del: other reasons-- https://master.php.net/note/delete/73750 Reject -- https://master.php.net/note/reject/73750 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124246) next »