<?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 => 'fr',
  ),
  'this' => 
  array (
    0 => 'function.exec.php',
    1 => 'exec',
    2 => 'Ex&eacute;cute un programme externe',
  ),
  'up' => 
  array (
    0 => 'ref.exec.php',
    1 => 'Fonctions sur l\'ex&eacute;cution de programmes externes',
  ),
  'prev' => 
  array (
    0 => 'function.escapeshellcmd.php',
    1 => 'escapeshellcmd',
  ),
  'next' => 
  array (
    0 => 'function.passthru.php',
    1 => 'passthru',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    '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">Exécute un programme externe</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.exec-description">
  <h3 class="title">Description</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> exécute la commande
   <code class="parameter">command</code>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.exec-parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">command</code></dt>
     <dd>
      <p class="para">
       La commande à exécuter.
      </p>
     </dd>
    
    
     <dt><code class="parameter">output</code></dt>
     <dd>
      <p class="para">
       Si l&#039;argument <code class="parameter">output</code> est présent,
       alors ce tableau sera rempli par les lignes retournées par
       la commande. Les espaces de début et de fin de chaîne, comme
       <code class="literal">\n</code>, ne seront pas inclus dans ce tableau.
       Il faut noter que si ce tableau contient des
       éléments, <span class="function"><strong>exec()</strong></span> ajoutera
       les nouvelles lignes à la fin du tableau. Si l&#039;on ne
       veut pas que les nouveaux éléments soient
       concaténés, utiliser la fonction
       <span class="function"><a href="function.unset.php" class="function">unset()</a></span> avec ce tableau avant
       de le passer à <span class="function"><strong>exec()</strong></span>.
      </p>
     </dd>
    
    
     <dt><code class="parameter">result_code</code></dt>
     <dd>
      <p class="para">
       Si l&#039;argument <code class="parameter">result_code</code> est présent
       en plus du tableau <code class="parameter">output</code>, alors le statut
       de retour d&#039;exécution sera inscrit dans cette variable.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.exec-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   La dernière ligne du résultat de la commande. Pour exécuter une commande
   et obtenir le résultat sans aucun traitement, il faut utiliser la
    fonction <span class="function"><a href="function.passthru.php" class="function">passthru()</a></span>.
  </p>
  <p class="para">
   Retourne <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> en cas d&#039;erreur.
  </p>
  <p class="para">
   Pour récupérer la sortie de la commande exécutée, il faut s&#039;assurer de définir
   et d&#039;utiliser le paramètre <code class="parameter">output</code>.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.exec-errors">
  <h3 class="title">Erreurs / Exceptions</h3>
  <p class="para">
   Émet une alerte <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> si <span class="function"><strong>exec()</strong></span> ne peut pas
   exécuter la commande <code class="parameter">command</code>.
  </p>
  <p class="para">
   Lève une exception <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> si <code class="parameter">command</code>
   est vide ou contient des octets nuls.
  </p>
</div>

<div class="refsect1 changelog" id="refsect1-function.exec-changelog">
  <h3 class="title">Historique</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       Si <code class="parameter">command</code> est vide ou contient des octets nuls,
       <span class="function"><strong>exec()</strong></span> lève désormais une exception <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span>.
       Précédemment, une alerte <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> était émise et retournait <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">Exemples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Exemple #1 Exemple avec <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">// Affiche le nom d'utilisateur qui fait tourner le processus php/http<br />// (sur un système ayant "whoami" dans le chemin d'exécutables)<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>Résultat de l&#039;exemple ci-dessus est similaire à :</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">Notes</h3>
  <div class="warning"><strong class="warning">Avertissement</strong><p class="para">
 Si des données provenant des utilisateurs ont la permission d&#039;être passées à cette fonction, utilisez
 <span class="function"><a href="function.escapeshellarg.php" class="function">escapeshellarg()</a></span> ou <span class="function"><a href="function.escapeshellcmd.php" class="function">escapeshellcmd()</a></span> pour s&#039;assurer que les utilisateurs
 ne peuvent pas amener le système à exécuter des commandes arbitraires.
</p></div>
  <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">
 Si un programme est démarré avec cette fonction et qu&#039;il tourne en arrière plan, la sortie du programme doit
 être redirigée vers un fichier, ou un autre flux de sortie. À défaut, PHP sera bloqué
 jusqu&#039;à la fin de l&#039;exécution du programme.
</p></p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">
 Sur Windows <span class="function"><strong>exec()</strong></span> démarrera d&#039;abord cmd.exe pour exécuter la commande.
 Si vous voulez démarrer un programme externe sans exécuter cmd.exe utilisez <span class="function"><a href="function.proc-open.php" class="function">proc_open()</a></span>
 en définissant l&#039;option <code class="parameter">bypass_shell</code>.
</p></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.exec-seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.system.php" class="function" rel="rdfs-seeAlso">system()</a> - Ex&eacute;cute un programme externe et affiche le r&eacute;sultat</span></li>
    <li><span class="function"><a href="function.passthru.php" class="function" rel="rdfs-seeAlso">passthru()</a> - Ex&eacute;cute un programme externe et affiche le r&eacute;sultat brut</span></li>
    <li><span class="function"><a href="function.escapeshellcmd.php" class="function" rel="rdfs-seeAlso">escapeshellcmd()</a> - Prot&egrave;ge les caract&egrave;res sp&eacute;ciaux du Shell</span></li>
    <li><span class="function"><a href="function.pcntl-exec.php" class="function" rel="rdfs-seeAlso">pcntl_exec()</a> - Ex&eacute;cute le programme indiqu&eacute; dans l'espace courant de processus</span></li>
    <li><a href="language.operators.execution.php" class="link">les guillemets obliques</a></li>
   </ul>
  </p>
 </div>

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