<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >system</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="Program Execution Functions" HREF="ref.exec.html"><LINK REL="PREVIOUS" TITLE="shell_exec" HREF="function.shell-exec.html"><LINK REL="NEXT" TITLE="PostScript document creation" HREF="ref.ps.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></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 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.shell-exec.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="ref.ps.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.system" ></A >system</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN189457" ></A ><P > (PHP 3, PHP 4, PHP 5)</P >system -- Execute an external program and display the output</DIV ><DIV CLASS="refsect1" ><A NAME="AEN189460" ></A ><H2 >说明</H2 >string <B CLASS="methodname" >system</B > ( string command [, int &return_var] )<BR ></BR ><P > <B CLASS="function" >system()</B > is just like the C version of the function in that it executes the given <CODE CLASS="parameter" >command</CODE > and outputs the result. </P ><P > The <B CLASS="function" >system()</B > call also tries to automatically flush the web server's output buffer after each line of output if PHP is running as a server module. </P ><P > If you need to execute a command and have all the data from the command passed directly back without any interference, use the <A HREF="function.passthru.html" ><B CLASS="function" >passthru()</B ></A > function. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN189478" ></A ><H2 >参数</H2 ><P > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><CODE CLASS="parameter" >command</CODE ></DT ><DD ><P > The command that will be executed. </P ></DD ><DT ><CODE CLASS="parameter" >return_var</CODE ></DT ><DD ><P > If the <CODE CLASS="parameter" >return_var</CODE > argument is present, then the return status of the executed command will be written to this variable. </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN189493" ></A ><H2 >返回值</H2 ><P > Returns the last line of the command output on success, and <TT CLASS="constant" ><B >FALSE</B ></TT > on failure. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN189497" ></A ><H2 >范例</H2 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN189500" ></A ><P ><B >例 1. <B CLASS="function" >system()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">echo </font><font color="#DD0000">'<pre>'</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Outputs all the result of shellcommand "ls", and returns<br />// the last output line into $last_line. Stores the return value<br />// of the shell command in $retval.<br /></font><font color="#0000BB">$last_line </font><font color="#007700">= </font><font color="#0000BB">system</font><font color="#007700">(</font><font color="#DD0000">'ls'</font><font color="#007700">, </font><font color="#0000BB">$retval</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Printing additional info<br /></font><font color="#007700">echo </font><font color="#DD0000">'<br /></pre><br /><hr />Last line of the output: ' </font><font color="#007700">. </font><font color="#0000BB">$last_line </font><font color="#007700">. </font><font color="#DD0000">'<br /><hr />Return value: ' </font><font color="#007700">. </font><font color="#0000BB">$retval</font><font color="#007700">;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN189504" ></A ><H2 >注释</H2 ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >警告</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >如果想允许用户输入的数据被传入本函数,则应使用 <A HREF="function.escapeshellarg.html" ><B CLASS="function" >escapeshellarg()</B ></A > 或 <A HREF="function.escapeshellcmd.html" ><B CLASS="function" >escapeshellcmd()</B ></A > 函数来确保用户不能欺骗系统从而执行任意命令。</P ></TD ></TR ></TABLE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B >如果用本函数启动一个程序并希望保持在后台运行,必须确保该程序的输出被重定向到一个文件或者其它输出流去,否则 PHP 会在程序执行结束前挂起。</P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B >在打开了<A HREF="features.safe-mode.html" >安全模式</A >时,只能执行在 <A HREF="features.safe-mode.html#ini.safe-mode-exec-dir" >safe_mode_exec_dir</A > 之内的程序。为实用起见目前不能在指向程序的路径中包含 <TT CLASS="literal" >..</TT > 成分。</P ></BLOCKQUOTE ></DIV ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >警告</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >在打开了<A HREF="features.safe-mode.html" >安全模式</A >时,命令字符串会被 <A HREF="function.escapeshellcmd.html" ><B CLASS="function" >escapeshellcmd()</B ></A > 转义。从而 <TT CLASS="literal" >echo y | echo x</TT > 就成了 <TT CLASS="literal" >echo "y | echo x"</TT >。</P ></TD ></TR ></TABLE ></DIV ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN189523" ></A ><H2 >参见</H2 ><P > <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD ><A HREF="function.exec.html" ><B CLASS="function" >exec()</B ></A ></TD ></TR ><TR ><TD ><A HREF="function.passthru.html" ><B CLASS="function" >passthru()</B ></A ></TD ></TR ><TR ><TD ><A HREF="function.popen.html" ><B CLASS="function" >popen()</B ></A ></TD ></TR ><TR ><TD ><A HREF="function.escapeshellcmd.html" ><B CLASS="function" >escapeshellcmd()</B ></A ></TD ></TR ><TR ><TD ><A HREF="function.pcntl-exec.html" ><B CLASS="function" >pcntl_exec()</B ></A ></TD ></TR ><TR ><TD ><A HREF="language.operators.execution.html" >backtick operator</A ></TD ></TR ></TBODY ></TABLE ><P ></P > </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.shell-exec.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="ref.ps.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >shell_exec</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.exec.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >PostScript document creation</TD ></TR ></TABLE ></DIV ></BODY ></HTML >