php-doc-en/reference/dir/functions/readdir.xml
Yannick Torres cdf0cec871 use more entities
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@171845 c90b9560-bf6c-de11-be94-00142212c4b1
2004-11-01 16:08:20 +00:00

111 lines
3 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.7 $ -->
<!-- splitted from ./en/functions/dir.xml, last change in rev 1.2 -->
<refentry id="function.readdir">
<refnamediv>
<refname>readdir</refname>
<refpurpose>Read entry from directory handle</refpurpose>
</refnamediv>
<refsect1>
&reftitle.description;
<methodsynopsis>
<type>string</type><methodname>readdir</methodname>
<methodparam><type>resource</type><parameter>dir_handle</parameter></methodparam>
</methodsynopsis>
<para>
Returns the filename of the next file from the directory. The
filenames are returned in the order in which they are stored by
the filesystem.
</para>
<para>
Please note the fashion in which <function>readdir</function>'s
return value is checked in the examples below. We are explicitly
testing whether the return value is identical to (equal to and of
the same type as--see <link
linkend="language.operators.comparison">Comparison
Operators</link> for more information) &false; since otherwise,
any directory entry whose name evaluates to &false; will stop the
loop (e.g. a directory named "0").
</para>
<para>
<example>
<title>List all files in a directory</title>
<programlisting role="php">
<![CDATA[
<?php
// Note that !== did not exist until 4.0.0-RC2
if ($handle = opendir('/path/to/files')) {
echo "Directory handle: $handle\n";
echo "Files:\n";
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
echo "$file\n";
}
/* This is the WRONG way to loop over the directory. */
while ($file = readdir($handle)) {
echo "$file\n";
}
closedir($handle);
}
?>
]]>
</programlisting>
</example>
</para>
<para>
Note that <function>readdir</function> will return the <literal>.</literal>
and
<literal>..</literal> entries. If you don't want these, simply strip
them out:
<example>
<title>
List all files in the current directory and strip out <literal>.</literal>
and <literal>..</literal>
</title>
<programlisting role="php">
<![CDATA[
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($handle);
}
?>
]]>
</programlisting>
</example>
</para>
<para>
See also <function>is_dir</function> and
<function>glob</function>.
</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:"../../../../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
-->