<HTML ><HEAD ><TITLE >popen</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Filesystem functions" HREF="ref.filesystem.html"><LINK REL="PREVIOUS" TITLE="pclose" HREF="function.pclose.html"><LINK REL="NEXT" TITLE="readfile" HREF="function.readfile.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.pclose.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.readfile.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.popen" ></A >popen</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN26774" ></A ><P > (PHP 3, PHP 4 )</P >popen -- Opens process file pointer</DIV ><DIV CLASS="refsect1" ><A NAME="AEN26777" ></A ><H2 >Description</H2 >resource <B CLASS="methodname" >popen</B > ( string command, string mode)<BR ></BR ><P > Opens a pipe to a process executed by forking the command given by command. </P ><P > Returns a file pointer identical to that returned by <A HREF="function.fopen.html" ><B CLASS="function" >fopen()</B ></A >, except that it is unidirectional (may only be used for reading or writing) and must be closed with <A HREF="function.pclose.html" ><B CLASS="function" >pclose()</B ></A >. This pointer may be used with <A HREF="function.fgets.html" ><B CLASS="function" >fgets()</B ></A >, <A HREF="function.fgetss.html" ><B CLASS="function" >fgetss()</B ></A >, and <A HREF="function.fputs.html" ><B CLASS="function" >fputs()</B ></A >. </P ><P > If an error occurs, returns <TT CLASS="constant" ><B >FALSE</B ></TT >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > If you're looking for bi-directional support (two-way), use <A HREF="function.proc-open.html" ><B CLASS="function" >proc_open()</B ></A >. </P ></BLOCKQUOTE ></DIV ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN26801" ></A ><P ><B >Example 1. <B CLASS="function" >popen()</B > examole</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $handle = popen ("/bin/ls", "r"); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > If the command to be executed could not be found, a valid resource is returned. This may seem odd, but makes sense; it allows you to access any error message returned by the shell: <DIV CLASS="informalexample" ><A NAME="AEN26807" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php error_reporting(E_ALL); /* Add redirection so we can get stderr. */ $handle = popen('/path/to/spooge 2>&1', 'r'); echo "'$handle'; " . gettype($handle) . "\n"; $read = fread($handle, 2096); echo $read; pclose($handle); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></BLOCKQUOTE ></DIV ><P > See also <A HREF="function.pclose.html" ><B CLASS="function" >pclose()</B ></A >, <A HREF="function.fopen.html" ><B CLASS="function" >fopen()</B ></A >, and <A HREF="function.proc-open.html" ><B CLASS="function" >proc_open()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.pclose.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.readfile.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >pclose</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.filesystem.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >readfile</TD ></TR ></TABLE ></DIV ></BODY ></HTML >