PHP proc_open()

1 개요[ | ]

PHP proc_open()
  • 명령어를 실행하고 입출력에 대한 파일 포인터를 여는 PHP 함수

2 예제 1[ | ]

<?php
$desc = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("file", "/tmp/error-output.txt", "a")
);
$process = proc_open("md5sum", $desc, $pipes);
if (is_resyntaxhighlight($process)) {
	$input = 'hello';
	fwrite($pipes[0], $input);
	fclose($pipes[0]);
	$output = stream_get_contents($pipes[1]);
	$exit_status = proc_close($process);

	echo "input: $input\n";
	echo "output: $output\n";
	echo "exit status: $exit_status\n";
}
# input: hello
# output: 5d41402abc4b2a76b9719d911017c592  -
# 
# exit status: 0

3 예제 2[ | ]

<?php
$desc = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("file", "/tmp/error-output.txt", "a")
);
$process = proc_open("not-exist-command", $desc, $pipes);
if (is_resyntaxhighlight($process)) {
	$input = 'hello';
	fwrite($pipes[0], $input);
	fclose($pipes[0]);
	$output = stream_get_contents($pipes[1]);
	$exit_status = proc_close($process);

	echo "input: $input\n";
	echo "output: $output\n";
	echo "exit status: $exit_status\n";
}
root@zetawiki:~# php proc_open_test1.php
input: hello
output: 
exit status: 127
root@zetawiki:~# cat /tmp/error-output.txt
sh: 1: not-exist-command: not found

4 같이 보기[ | ]

5 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}