PHP popen() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, popen() runs a shell command and opens a pipe to it. You can then read the output of the command or send data to it, just like with a file.

Use mode r to read what the command prints, or mode w to write data into the command. Always close the pipe with pclose() when you are done.

Syntax

Call popen() with the values you need. Start with this line.

PHP
$handle = popen('echo CodeToFun', 'r');
CallWhat it does
popen('echo CodeToFun', 'r')Runs the command and lets PHP read its output.
popen('sort', 'w')Starts the sort command and lets PHP send lines into it.
popen('ls -l', 'r')Reads a folder listing on Linux or macOS.

A pipe works in one direction only, so use r or w, not both. If you need to both write and read, use proc_open(). Some hosts turn off popen() for security, so check function_exists('popen') if your code must run everywhere.

Library declarations
PHP
popen(string $command, string $mode): resource|false

Parameters

ParameterDescription
commandRequired. The shell command to run.
modeRequired. Either r (read) or w (write). The letter b can be added for binary mode on Windows.

Return Value

Return ValueDescription
resource|falseReturns a file pointer on success, or false on failure.

PHP Examples

Example 1 — Read the Output of a Command

Run echo and read the line that it prints.

PHP
<?php
$handle = popen('echo CodeToFun', 'r');

if ($handle !== false) {
    echo fgets($handle);
    pclose($handle);
}

How It Works

  1. popen() starts the command and opens a pipe in read mode.
  2. The if check makes sure the pipe was really opened.
  3. fgets() reads one line from the pipe, which is the text the command printed.
  4. pclose() closes the pipe and waits for the command to finish.

Example 2 — An Invalid Mode

In PHP 8, a mode other than r or w throws a ValueError.

PHP
<?php
try {
    $handle = popen('echo hi', 'x');
} catch (ValueError $e) {
    echo "Error: ", $e->getMessage(), PHP_EOL;
}

How It Works

  1. x is not a valid mode for a pipe.
  2. popen() rejects it by throwing a ValueError.
  3. The catch block receives the error and prints its message.
  4. Use r to read or w to write.

Example 3 — Send Data into a Command

Open the sort command in write mode and send it some unsorted words.

PHP
<?php
$handle = popen('sort', 'w');

fwrite($handle, 'pear' . PHP_EOL);
fwrite($handle, 'apple' . PHP_EOL);
fwrite($handle, 'banana' . PHP_EOL);

pclose($handle);

How It Works

  1. popen() starts the sort command and opens a pipe in write mode.
  2. fwrite() sends each word to the command, one per line.
  3. pclose() tells sort that the input is finished, so it prints the sorted list.
  4. The output of sort goes straight to the screen, not back into PHP.

Frequently Asked Questions

It runs a shell command and opens a pipe so you can read its output or send input to it.
Yes. pclose() closes the pipe, waits for the command to end and returns its exit status.
No. A pipe is one way only. Use proc_open() when you need both input and output.
Only when the command is fixed or built with escapeshellarg(). Never place raw user input in the command string.
Many shared hosts disable it in the disable_functions setting of php.ini. Ask your host or use a different approach.

Next: readfile()

readfile() reads a file and writes its content straight to the output. It does not store t.

readfile() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful