PHP Function Reference

PHP pclose() Function



The PHP pclose() function closes a file pointer to a pipe opened by popen(). This function returns the termination status of the process that was run. In case of an error then -1 is returned.

Syntax

popen(handle)

Parameters

handle Required. Specify the file pointer to close. It must be valid, and must have been returned by a successful call to popen().

Return Value

Returns the termination status of the process that was run. In case of an error then -1 is returned.

Example: pclose() example

The example below shows how to use this function to close a pipe opened by popen().

<?php
//opening a pipe
$handle = popen("/bin/ls", "r");

//some code to be executed

//closing the pipe
pclose($handle);
?>

Please note that if the command to be executed could not be found, a valid resource is returned.

Example: pclose() example

Consider one more example which demonstrates the usage of this function.

<?php
error_reporting(E_ALL);

//opening a pipe
$handle = popen('/path/to/executable 2>&1', 'r');

echo "'$handle'; " . gettype($handle) . "\n";
$read = fread($handle, 2096);
echo $read;

//closing the pipe
pclose($handle);
?>

❮ PHP Filesystem Reference