<?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 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.exec.php',
    1 => 'exec',
    2 => '执行一个外部程序',
  ),
  'up' => 
  array (
    0 => 'ref.exec.php',
    1 => '程序执行函数',
  ),
  'prev' => 
  array (
    0 => 'function.escapeshellcmd.php',
    1 => 'escapeshellcmd',
  ),
  'next' => 
  array (
    0 => 'function.passthru.php',
    1 => 'passthru',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/exec/functions/exec.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.exec" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">exec</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">exec</span> &mdash; <span class="dc-title">执行一个外部程序</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.exec-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>exec</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$command</code></span>, <span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter reference">&$output</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter reference">&$result_code</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>): <span class="type"><span class="type"><a href="language.types.string.php" class="type string">string</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>exec()</strong></span> 执行
   <code class="parameter">command</code> 参数所指定的命令。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.exec-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">command</code></dt>
     <dd>
      <p class="para">
       要执行的命令。
      </p>
     </dd>
    
    
     <dt><code class="parameter">output</code></dt>
     <dd>
      <p class="para">
        如果提供了 <code class="parameter">output</code> 参数，
        那么会用命令执行的输出填充此数组，
        每行输出填充数组中的一个元素。
        数组中的数据不包含行尾的空白字符，例如 <code class="literal">\n</code> 字符。
        请注意，如果数组中已经包含了部分元素，<span class="function"><strong>exec()</strong></span>
        函数会在数组末尾追加内容。如果你不想在数组末尾进行追加，
        请在传入 <span class="function"><strong>exec()</strong></span> 函数之前
        对数组使用 <span class="function"><a href="function.unset.php" class="function">unset()</a></span> 函数进行重置。
      </p>
     </dd>
    
    
     <dt><code class="parameter">result_code</code></dt>
     <dd>
      <p class="para">
        如果同时提供 
        <code class="parameter">output</code> 和 <code class="parameter">result_code</code>
        参数，命令执行后的返回状态会被写入到此变量。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.exec-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
    命令执行结果的最后一行内容。
    如果你需要获取未经处理的全部输出数据，
    请使用 <span class="function"><a href="function.passthru.php" class="function">passthru()</a></span> 函数。
  </p>
  <p class="para">
   失败时返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
  </p>
  <p class="para">
    如果想要获取命令的输出内容，
    请确保使用 <code class="parameter">output</code> 参数。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.exec-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   如果 <span class="function"><strong>exec()</strong></span> 无法执行 <code class="parameter">command</code>，会发出 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>。
  </p>
  <p class="para">
   如果 <code class="parameter">command</code> 为空或者包含 null 字节，则抛出 <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.exec-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       如果 <code class="parameter">command</code> 为空或者包含 null 字节，<span class="function"><strong>exec()</strong></span> 现在将抛出
       <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span>。之前会发出 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> 并返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.exec-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>exec()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// 输出运行中的 php/httpd 进程的创建者用户名<br />// （在可以执行 "whoami" 命令的系统上）<br /></span><span style="color: #0000BB">$output</span><span style="color: #007700">=</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$retval</span><span style="color: #007700">=</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">'whoami'</span><span style="color: #007700">, </span><span style="color: #0000BB">$output</span><span style="color: #007700">, </span><span style="color: #0000BB">$retval</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"Returned with status </span><span style="color: #0000BB">$retval</span><span style="color: #DD0000"> and output:\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$output</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>以上示例的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Returned with status 0 and output:
Array
(
    [0] =&gt; cmb
)</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.exec-notes">
  <h3 class="title">注释</h3>
  <div class="warning"><strong class="warning">警告</strong><p class="para">当传入用户提供的数据到本函数时，应使用
<span class="function"><a href="function.escapeshellarg.php" class="function">escapeshellarg()</a></span> 或 <span class="function"><a href="function.escapeshellcmd.php" class="function">escapeshellcmd()</a></span>
来防止用户欺骗系统执行任意命令。</p></div>
  <blockquote class="note"><p><strong class="note">注意</strong>: <p class="para">如何程序使用此函数启动，为了能保持在后台运行，此程序必须将输出重定向到文件或其它输出流。否则会导致
PHP 挂起，直至程序执行结束。</p></p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: <p class="para">在 Windows 下，<span class="function"><strong>exec()</strong></span>
函数会先启动 cmd.exe 来执行指定的命令。如果你希望启动一个额外的应用而不用先启动 cmd.exe
可使用 <span class="function"><a href="function.proc-open.php" class="function">proc_open()</a></span> 函数加上 <code class="parameter">bypass_shell</code> 参数。</p></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.exec-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.system.php" class="function" rel="rdfs-seeAlso">system()</a> - 执行外部程序，并且显示输出</span></li>
    <li><span class="function"><a href="function.passthru.php" class="function" rel="rdfs-seeAlso">passthru()</a> - 执行外部程序并且显示原始输出</span></li>
    <li><span class="function"><a href="function.escapeshellcmd.php" class="function" rel="rdfs-seeAlso">escapeshellcmd()</a> - shell 元字符转义</span></li>
    <li><span class="function"><a href="function.pcntl-exec.php" class="function" rel="rdfs-seeAlso">pcntl_exec()</a> - 在当前进程空间执行指定程序</span></li>
    <li><a href="language.operators.execution.php" class="link">执行运算符</a></li>
   </ul>
  </p>
 </div>

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