<HTML ><HEAD ><TITLE >readdir</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Directory functions" HREF="ref.dir.html"><LINK REL="PREVIOUS" TITLE="opendir" HREF="function.opendir.html"><LINK REL="NEXT" TITLE="rewinddir" HREF="function.rewinddir.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></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 Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.opendir.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.rewinddir.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.readdir" ></A >readdir</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN20057" ></A ><P > (PHP 3, PHP 4 )</P >readdir -- read entry from directory handle</DIV ><DIV CLASS="refsect1" ><A NAME="AEN20060" ></A ><H2 >Description</H2 >string <B CLASS="methodname" >readdir</B > ( resource dir_handle)<BR ></BR ><P > 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. </P ><P > Please note the fashion in which <B CLASS="function" >readdir()</B >'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 <A HREF="language.operators.comparison.html" >Comparison Operators</A > for more information) <TT CLASS="constant" ><B >FALSE</B ></TT > since otherwise, any directory entry whose name evaluates to <TT CLASS="constant" ><B >FALSE</B ></TT > will stop the loop (e.g. a directory named "0"). </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN20075" ></A ><P ><B >Example 1. List all files in a directory</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >// Note that !== did not exist until 4.0.0-RC2 <?php 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); } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > Note that <B CLASS="function" >readdir()</B > will return the <TT CLASS="literal" >.</TT > and <TT CLASS="literal" >..</TT > entries. If you don't want these, simply strip them out: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN20082" ></A ><P ><B >Example 2. List all files in the current directory and strip out <TT CLASS="literal" >.</TT > and <TT CLASS="literal" >..</TT > </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { echo "$file\n"; } } closedir($handle); } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P >See also <A HREF="function.is-dir.html" ><B CLASS="function" >is_dir()</B ></A >.</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.opendir.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.rewinddir.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >opendir</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.dir.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >rewinddir</TD ></TR ></TABLE ></DIV ></BODY ></HTML >