<HTML ><HEAD ><TITLE >readdir</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.44"><LINK REL="HOME" TITLE="PHP Manual" HREF="manual.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"></HEAD ><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><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" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.rewinddir.html" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.readdir" >readdir</A ></H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7766" ></A >readdir -- read entry from directory handle</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7769" ></A ><H2 >Description</H2 ><DIV CLASS="funcsynopsis" ><P ></P ><CODE CLASS="FUNCDEF" >string readdir</CODE >(int dir_handle);<P ></P ></DIV ><P > Returns the filename of the next file from the directory. The filenames are not returned in any particular order. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><P ><B >Example 1. List all files in the current directory</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="programlisting" > 1 2 <?php 3 $handle=opendir('.'); 4 echo "Directory handle: $handle\n"; 5 echo "Files:\n"; 6 while ($file = readdir($handle)) { 7 echo "$file\n"; 8 } 9 closedir($handle); 10 ?> 11 </PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > Note that <B CLASS="function" >readdir()</B > will return the . and .. 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" ><P ><B >Example 2. List all files in the current directory and strip out . and .. </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="programlisting" > 1 2 <?php 3 $handle=opendir('.'); 4 while ($file = readdir($handle)) { 5 if ($file != "." && $file != "..") { 6 echo "$file\n"; 7 } 8 } 9 closedir($handle); 10 ?> 11 </PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.opendir.html" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="manual.html" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.rewinddir.html" >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" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >rewinddir</TD ></TR ></TABLE ></DIV ></BODY ></HTML >