Content · Programming · PHP

PHP proc_open() Function: Usage and Examples

Learn PHP proc_open() through clear code examples, practical usage, important limitations and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

proc_open() starts a child process with configurable stdin, stdout and stderr pipes. This guide explains the function with focused examples and the decisions that matter in real applications.

What Is proc_open() in PHP?

Starts a child process with configurable stdin, stdout and stderr pipes.

A typical call looks like this:

<?php

proc_open($command, $descriptor_spec, &$pipes, [$cwd], [$env_vars], [$options]);

The input arguments shown in the call identify the relevant connection, resource, value, path or configuration for the operation. Pay attention to argument order and to the return value demonstrated below.

Example 1: Basic Usage

This first example uses proc_open() directly to demonstrate its basic purpose.

<?php

$process = proc_open(["php", "-r", "echo 42;"], [1 => ["pipe", "w"]], $pipes); echo stream_get_contents($pipes[1]); fclose($pipes[1]); proc_close($process);

The returned value illustrates starts a child process with configurable stdin, stdout and stderr pipes. Avoid shell injection; use an argument array and handle pipes and exit status safely.

Example 2: A Different Input or Option

The next example changes the input or option, showing another supported use of proc_open().

<?php

$process = proc_open(["php", "-r", "fwrite(STDERR, 123);"], [2 => ["pipe", "w"]], $pipes); echo stream_get_contents($pipes[2]); fclose($pipes[2]); proc_close($process);

The returned value illustrates starts a child process with configurable stdin, stdout and stderr pipes. Avoid shell injection; use an argument array and handle pipes and exit status safely.

Practical Example: Applying the Function

In application code, proc_open() belongs in a controlled workflow with input validation, a checked result and cleanup where needed.

<?php

$command = ["php", "-r", "echo 456;"]; $process = proc_open($command, [1 => ["pipe", "w"]], $pipes); $output = stream_get_contents($pipes[1]); fclose($pipes[1]); $exitCode = proc_close($process); echo "$output ($exitCode)";

The returned value illustrates starts a child process with configurable stdin, stdout and stderr pipes. Avoid shell injection; use an argument array and handle pipes and exit status safely.

Important Behavior and Edge Cases

  • Main behavior: Starts a child process with configurable stdin, stdout and stderr pipes.
  • Important limitation: Avoid shell injection; use an argument array and handle pipes and exit status safely.
  • Requirements: The behavior and privileges of child processes vary across systems.

Comparison With Related Functions

Function or ApproachWhen to Use It
proc_open()Starts a child process with configurable stdin, stdout and stderr pipes.
popenStarts a process using a shell command and opens a pipe to its input or output.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the specific limits or preconditions of proc_open().Avoid shell injection; use an argument array and handle pipes and exit status safely.
Assuming every environment produces the same result.The behavior and privileges of child processes vary across systems.

Key Takeaways

  • Starts a child process with configurable stdin, stdout and stderr pipes.
  • Avoid shell injection; use an argument array and handle pipes and exit status safely.
  • The behavior and privileges of child processes vary across systems.

Use proc_open() when its behavior matches your task, and verify the relevant return value before taking further action. Run examples that affect processes, users, privileges or the filesystem only in a controlled development environment.

Share this resourceSend this page to someone who may find it useful.