<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.exec.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.proc-open.php',
    1 => 'proc_open',
    2 => 'Execute a command and open file pointers for input/output',
  ),
  'up' => 
  array (
    0 => 'ref.exec.php',
    1 => 'Program execution Functions',
  ),
  'prev' => 
  array (
    0 => 'function.proc-nice.php',
    1 => 'proc_nice',
  ),
  'next' => 
  array (
    0 => 'function.proc-terminate.php',
    1 => 'proc_terminate',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/exec/functions/proc-open.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.proc-open" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">proc_open</h1>
    <p class="verinfo">(PHP 4 &gt;= 4.3.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">proc_open</span> &mdash; <span class="dc-title">
     Execute a command and open file pointers for input/output
    </span></p>

   </div>
   <div class="refsect1 description" id="refsect1-function.proc-open-description">
    <h3 class="title">Description</h3>
     <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>proc_open</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">$command</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$descriptor_spec</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter reference">&$pipes</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">$cwd</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.array.php" class="type array">array</a></span></span> <code class="parameter">$env_vars</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.array.php" class="type array">array</a></span></span> <code class="parameter">$options</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span><br>): <span class="type"><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

    <p class="para rdfs-comment">
     <span class="function"><strong>proc_open()</strong></span> is similar to <span class="function"><a href="function.popen.php" class="function">popen()</a></span>
     but provides a much greater degree of control over the program execution.
    </p>



   </div>


 <div class="refsect1 parameters" id="refsect1-function.proc-open-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">command</code></dt>
     <dd>
      <p class="para">
       The commandline to execute as <span class="type"><a href="language.types.string.php" class="type string">string</a></span>. Special characters have to be properly escaped,
       and proper quoting has to be applied.
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <span class="simpara">
        On <em>Windows</em>, unless <code class="literal">bypass_shell</code> is set to <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> in
        <code class="parameter">options</code>, the <code class="parameter">command</code> is
        passed to <strong class="command">cmd.exe</strong> (actually, <code class="literal">%ComSpec%</code>)
        with the <code class="literal">/c</code> flag as <em>unquoted</em> string
        (i.e. exactly as has been given to <span class="function"><strong>proc_open()</strong></span>).
        This can cause <strong class="command">cmd.exe</strong> to remove enclosing quotes from
        <code class="parameter">command</code> (for details see the <strong class="command">cmd.exe</strong> documentation),
        resulting in unexpected, and potentially even dangerous behavior, because
        <strong class="command">cmd.exe</strong> error messages may contain (parts of) the passed
        <code class="parameter">command</code> (see example below).
       </span>
      </p></blockquote>
      <p class="para">
       As of PHP 7.4.0, <code class="parameter">command</code> may be passed as <span class="type"><a href="language.types.array.php" class="type array">array</a></span> of command parameters.
       In this case the process will be opened directly (without going through a shell)
       and PHP will take care of any necessary argument escaping.
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        On Windows, the argument escaping of the <span class="type"><a href="language.types.array.php" class="type array">array</a></span> elements assumes that the
        command line parsing of the executed command is compatible with the parsing
        of command line arguments done by the VC runtime.
       </p>
      </p></blockquote>
     </dd>
    
    
     <dt><code class="parameter">descriptor_spec</code></dt>
     <dd>
      <p class="para">
       An indexed array where the key represents the descriptor number and the
       value represents how PHP will pass that descriptor to the child
       process. 0 is stdin, 1 is stdout, while 2 is stderr.
      </p>
      <p class="para">
       Each element can be:
       <ul class="simplelist">
        <li>An array describing the pipe to pass to the process. The first
         element is the descriptor type and the second element is an option for
         the given type. Valid types are <code class="literal">pipe</code> (the second
         element is either <code class="literal">r</code> to pass the read end of the pipe
         to the process, or <code class="literal">w</code> to pass the write end) and
         <code class="literal">file</code> (the second element is a filename).
         Note that anything else than <code class="literal">w</code> is treated like <code class="literal">r</code>.
        </li>
        <li>
         A stream resource representing a real file descriptor (e.g. opened file,
         a socket, <strong><code><a href="reserved.constants.php#constant.stdin">STDIN</a></code></strong>).
        </li>
       </ul>
      </p>
      <p class="para">
       The file descriptor numbers are not limited to 0, 1 and 2 - you may
       specify any valid file descriptor number and it will be passed to the
       child process. This allows your script to interoperate with other
       scripts that run as &quot;co-processes&quot;. In particular, this is useful for
       passing passphrases to programs like PGP, GPG and openssl in a more
       secure manner. It is also useful for reading status information
       provided by those programs on auxiliary file descriptors.
      </p>
     </dd>
    
    
     <dt><code class="parameter">pipes</code></dt>
     <dd>
      <p class="para">
       Will be set to an indexed array of file pointers that correspond to
       PHP&#039;s end of any pipes that are created.
      </p>
     </dd>
    
    
     <dt><code class="parameter">cwd</code></dt>
     <dd>
      <p class="para">
       The initial working dir for the command. This must be an
       <strong>absolute</strong> directory path, or <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>
       if you want to use the default value (the working dir of the current
       PHP process)
      </p>
     </dd>
    
    
     <dt><code class="parameter">env_vars</code></dt>
     <dd>
      <p class="para">
       An array with the environment variables for the command that will be
       run, or <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> to use the same environment as the current PHP process
      </p>
     </dd>
    
    
     <dt><code class="parameter">options</code></dt>
     <dd>
      <p class="para">
       Allows you to specify additional options. Currently supported options
       include:
       <ul class="simplelist">
        <li>
         <code class="literal">suppress_errors</code> (windows only): suppresses errors
         generated by this function when it's set to <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>
        </li>
        <li>
         <code class="literal">bypass_shell</code> (windows only): bypass
         <code class="literal">cmd.exe</code> shell when set to <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>
        </li>
        <li>
         <code class="literal">blocking_pipes</code> (windows only): force
         blocking pipes when set to <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>
        </li>
        <li>
         <code class="literal">create_process_group</code> (windows only): allow the
         child process to handle <code class="literal">CTRL</code> events when set to <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>
        </li>
        <li>
         <code class="literal">create_new_console</code> (windows only): the new process
         has a new console, instead of inheriting its parent's console
        </li>
       </ul>
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.proc-open-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns a resource representing the process, which should be freed using
   <span class="function"><a href="function.proc-close.php" class="function">proc_close()</a></span> when you are finished with it. On failure
   returns <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.proc-open-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="simpara">
   As of PHP 8.3.0, throws a <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> if
   <code class="parameter">command</code> is an array without at least one
   non-empty element.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.proc-open-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.3.0</td>
       <td>
        A <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> will be thrown if
        <code class="parameter">command</code> is an array without at least one
        non-empty element.
       </td>
      </tr>

      <tr>
       <td>7.4.4</td>
       <td>
        Added the <code class="literal">create_new_console</code> option to the
        <code class="parameter">options</code> parameter.
       </td>
      </tr>

      <tr>
       <td>7.4.0</td>
       <td>
        <span class="function"><strong>proc_open()</strong></span> now also accepts an <span class="type"><a href="language.types.array.php" class="type array">array</a></span>
        for the <code class="parameter">command</code>.
       </td>
      </tr>

      <tr>
       <td>7.4.0</td>
       <td>
        Added the <code class="literal">create_process_group</code> option to the
        <code class="parameter">options</code> parameter.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.proc-open-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 A <span class="function"><strong>proc_open()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$descriptorspec </span><span style="color: #007700">= array(<br />   </span><span style="color: #0000BB">0 </span><span style="color: #007700">=&gt; array(</span><span style="color: #DD0000">"pipe"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">),  </span><span style="color: #FF8000">// stdin is a pipe that the child will read from<br />   </span><span style="color: #0000BB">1 </span><span style="color: #007700">=&gt; array(</span><span style="color: #DD0000">"pipe"</span><span style="color: #007700">, </span><span style="color: #DD0000">"w"</span><span style="color: #007700">),  </span><span style="color: #FF8000">// stdout is a pipe that the child will write to<br />   </span><span style="color: #0000BB">2 </span><span style="color: #007700">=&gt; array(</span><span style="color: #DD0000">"file"</span><span style="color: #007700">, </span><span style="color: #DD0000">"/tmp/error-output.txt"</span><span style="color: #007700">, </span><span style="color: #DD0000">"a"</span><span style="color: #007700">) </span><span style="color: #FF8000">// stderr is a file to write to<br /></span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$cwd </span><span style="color: #007700">= </span><span style="color: #DD0000">'/tmp'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$env </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'some_option' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'aeiou'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$process </span><span style="color: #007700">= </span><span style="color: #0000BB">proc_open</span><span style="color: #007700">(</span><span style="color: #DD0000">'php'</span><span style="color: #007700">, </span><span style="color: #0000BB">$descriptorspec</span><span style="color: #007700">, </span><span style="color: #0000BB">$pipes</span><span style="color: #007700">, </span><span style="color: #0000BB">$cwd</span><span style="color: #007700">, </span><span style="color: #0000BB">$env</span><span style="color: #007700">);<br /><br />if (</span><span style="color: #0000BB">is_resource</span><span style="color: #007700">(</span><span style="color: #0000BB">$process</span><span style="color: #007700">)) {<br />    </span><span style="color: #FF8000">// $pipes now looks like this:<br />    // 0 =&gt; writeable handle connected to child stdin<br />    // 1 =&gt; readable handle connected to child stdout<br />    // Any error output will be appended to /tmp/error-output.txt<br /><br />    </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$pipes</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">], </span><span style="color: #DD0000">'&lt;?php print_r($_ENV); ?&gt;'</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$pipes</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br /><br />    echo </span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$pipes</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br />    </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$pipes</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br /><br />    </span><span style="color: #FF8000">// It is important that you close any pipes before calling<br />    // proc_close in order to avoid a deadlock<br />    </span><span style="color: #0000BB">$return_value </span><span style="color: #007700">= </span><span style="color: #0000BB">proc_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$process</span><span style="color: #007700">);<br /><br />    echo </span><span style="color: #DD0000">"command returned </span><span style="color: #0000BB">$return_value</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output
something similar to:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Array
(
    [some_option] =&gt; aeiou
    [PWD] =&gt; /tmp
    [SHLVL] =&gt; 1
    [_] =&gt; /usr/local/bin/php
)
command returned 0</pre>
</div>
    </div>
   </div>
  </p>

  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Example #2 <span class="function"><strong>proc_open()</strong></span> quirk on Windows</strong></p>
    <div class="example-contents"><p>
     While one may expect the following program to search the file
     <var class="filename">filename.txt</var> for the text <code class="literal">search</code> and
     to print the results, it behaves rather differently.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$descriptorspec </span><span style="color: #007700">= [</span><span style="color: #0000BB">STDIN</span><span style="color: #007700">, </span><span style="color: #0000BB">STDOUT</span><span style="color: #007700">, </span><span style="color: #0000BB">STDOUT</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$cmd </span><span style="color: #007700">= </span><span style="color: #DD0000">'"findstr" "search" "filename.txt"'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$proc </span><span style="color: #007700">= </span><span style="color: #0000BB">proc_open</span><span style="color: #007700">(</span><span style="color: #0000BB">$cmd</span><span style="color: #007700">, </span><span style="color: #0000BB">$descriptorspec</span><span style="color: #007700">, </span><span style="color: #0000BB">$pipes</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">proc_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$proc</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">&#039;findstr&quot; &quot;search&quot; &quot;filename.txt&#039; is not recognized as an internal or external command,
operable program or batch file.</pre>
</div>
    </div>
    <div class="example-contents"><p>
     To work around that behavior, it is usually sufficient to enclose the
     <code class="parameter">command</code> in additional quotes:
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">$cmd = '""findstr" "search" "filename.txt""';</span></code></div>
    </div>

   </div>
  </p>



 </div>


 <div class="refsect1 notes" id="refsect1-function.proc-open-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Windows compatibility: Descriptors beyond 2 (stderr) are made available to
    the child process as inheritable handles, but since the Windows
    architecture does not associate file descriptor numbers with low-level
    handles, the child process does not (yet) have a means of accessing those
    handles. Stdin, stdout and stderr work as expected.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
   If you only need a uni-directional (one-way) process pipe, use
   <span class="function"><a href="function.popen.php" class="function">popen()</a></span> instead, as it is much easier to use.
   </p>
  </p></blockquote>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.proc-open-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.popen.php" class="function" rel="rdfs-seeAlso">popen()</a> - Opens process file pointer</span></li>
    <li><span class="function"><a href="function.exec.php" class="function" rel="rdfs-seeAlso">exec()</a> - Execute an external program</span></li>
    <li><span class="function"><a href="function.system.php" class="function" rel="rdfs-seeAlso">system()</a> - Execute an external program and display the output</span></li>
    <li><span class="function"><a href="function.passthru.php" class="function" rel="rdfs-seeAlso">passthru()</a> - Execute an external program and display raw output</span></li>
    <li><span class="function"><a href="function.stream-select.php" class="function" rel="rdfs-seeAlso">stream_select()</a> - Runs the equivalent of the select() system call on the given
   arrays of streams with a timeout specified by seconds and microseconds</span></li>
    <li>The <a href="language.operators.execution.php" class="link">backtick operator</a></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>