php-doc-en/reference/filesystem/functions/popen.xml
2011-02-10 12:53:33 +00:00

146 lines
3.5 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xmlns="http://docbook.org/ns/docbook" xml:id="function.popen">
<refnamediv>
<refname>popen</refname>
<refpurpose>Opens process file pointer</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>resource</type><methodname>popen</methodname>
<methodparam><type>string</type><parameter>command</parameter></methodparam>
<methodparam><type>string</type><parameter>mode</parameter></methodparam>
</methodsynopsis>
<para>
Opens a pipe to a process executed by forking the command given
by command.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>command</parameter></term>
<listitem>
<para>
The command
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>mode</parameter></term>
<listitem>
<para>
The mode
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns a file pointer identical to that returned by
<function>fopen</function>, except that it is unidirectional (may
only be used for reading or writing) and must be closed with
<function>pclose</function>. This pointer may be used with
<function>fgets</function>, <function>fgetss</function>, and
<function>fwrite</function>. When the mode is 'r', the returned
file pointer equals to the STDOUT of the command, when the mode
is 'w', the returned file pointer equals to the STDIN of the
command.
</para>
<para>
If an error occurs, returns &false;.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>popen</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
$handle = popen("/bin/ls", "r");
?>
]]>
</programlisting>
</example>
</para>
<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:
<example>
<title><function>popen</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
error_reporting(E_ALL);
/* Add redirection so we can get stderr. */
$handle = popen('/path/to/executable 2>&1', 'r');
echo "'$handle'; " . gettype($handle) . "\n";
$read = fread($handle, 2096);
echo $read;
pclose($handle);
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<note>
<para>
If you're looking for bi-directional support (two-way), use
<function>proc_open</function>.
</para>
</note>
&note.exec-path;
&warn.sm.exec;
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>pclose</function></member>
<member><function>fopen</function></member>
<member><function>proc_open</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->