📄 function.popen.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Opens process file pointer</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.pclose.html">pclose</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.readfile.html">readfile</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.popen" class="refentry"> <div class="refnamediv"> <h1 class="refname">popen</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">popen</span> — <span class="dc-title">Opens process file pointer</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">resource</span> <span class="methodname"><b><b>popen</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$command</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$mode</tt></span> )</div> <p class="para rdfs-comment"> Opens a pipe to a process executed by forking the command given by command. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">command</tt></i></span> <dd> <p class="para"> The command </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">mode</tt></i></span> <dd> <p class="para"> The mode </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns a file pointer identical to that returned by <a href="function.fopen.html" class="function">fopen()</a>, except that it is unidirectional (may only be used for reading or writing) and must be closed with <a href="function.pclose.html" class="function">pclose()</a>. This pointer may be used with <a href="function.fgets.html" class="function">fgets()</a>, <a href="function.fgetss.html" class="function">fgetss()</a>, and <a href="function.fwrite.html" class="function">fwrite()</a>. </p> <p class="para"> If an error occurs, returns <b><tt>FALSE</tt></b>. </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>popen()</b> example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">popen</span><span style="color: #007700">(</span><span style="color: #DD0000">"/bin/ls"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> 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="example"> <p><b>Example #2 <b>popen()</b> example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ALL</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Add redirection so we can get stderr. */<br /></span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">popen</span><span style="color: #007700">(</span><span style="color: #DD0000">'/path/to/spooge 2>&1'</span><span style="color: #007700">, </span><span style="color: #DD0000">'r'</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"'$handle'; " </span><span style="color: #007700">. </span><span style="color: #0000BB">gettype</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$read </span><span style="color: #007700">= </span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">, </span><span style="color: #0000BB">2096</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$read</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">pclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div> <div class="refsect1 notes"> <h3 class="title">Notes</h3> <blockquote><p><b class="note">Note</b>: If you're looking for bi-directional support (two-way), use <a href="function.proc-open.html" class="function">proc_open()</a>. <br /> </p></blockquote> <blockquote><p><b class="note">Note</b>: <span class="simpara">When<a href="features.safe-mode.html" class="link">safe mode</a> is enabled, you can onlyexecute files within the <a href="features.safe-mode.html#ini.safe-mode-exec-dir" class="link">safe_mode_exec_dir</a>.For practical reasons, it is currently not allowed to have <i>..</i>components in the path to the executable.</span></p></blockquote> <div class="warning"><b class="warning">Warning</b><p class="simpara">With <a href="features.safe-mode.html" class="link">safe mode</a> enabled,the command string is escaped with <a href="function.escapeshellcmd.html" class="function">escapeshellcmd()</a>. Thus, <i>echo y | echo x</i> becomes <i>echo y \| echo x</i>.</p></div> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.pclose.html" class="function" rel="rdfs-seeAlso">pclose()</a></li> <li class="member"><a href="function.fopen.html" class="function" rel="rdfs-seeAlso">fopen()</a></li> <li class="member"><a href="function.proc-open.html" class="function" rel="rdfs-seeAlso">proc_open()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.pclose.html">pclose</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.readfile.html">readfile</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -