<?xml version="1.0" encoding="utf-8"?>
<tutorial>

<description>Learn how to make a directory list in PHP.</description>
<keywords>tutorial, PHP, tutorials, directory, listing, folder, show, display</keywords>
<title>Directory List</title>

<slug>Learn how to make a directory list in PHP.</slug>

<text>
A simple script that lists all the items in the directory including folders and files and even makes a link out of them. Its an all purpose script which you can use to do other things. You can see my code that I worked on and made sure worked by right clicking and saving <link><url>list.txt</url>list.txt</link>
<br></br><br></br>
Here is what we have to write in English:
<ol>
	<li>Define the Path which you want to have the directory displayed and save it as a variable.</li>
	<li>Use the <strong>@opendir()</strong> function to define the array.</li>
	<li>Run a while loop to read everything from the array.</li>
	<li>Display the results and set them up like links.</li>
	<li>Close the array.</li>
</ol>
Here it is in PHP:
<![CDATA[
<pre>
&lt;?

//define the path as relative
$path = "/home/yoursite/public_html/whatever";

//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");

echo "Directory Listing of $path&lt;br/&gt;";

//running the while loop
while ($file = readdir($dir_handle)) 
{
   echo "&lt;a href='$file'&gt;$file&lt;/a&gt;&lt;br/&gt;";
}

//closing the directory
closedir($dir_handle);

?&gt; 
</pre>
]]>
One of the questions people have asked is how to not display the double dots and single dots. This can be done by modifying the while loop and replacing it with:
<![CDATA[
<pre>
while ($file = readdir($dir_handle)) 
{
   if($file!="." && $file!="..")
      echo "&lt;a href='$file'&gt;$file&lt;/a&gt;&lt;br/&gt;";
}
</pre>
]]>
Not to hard was it? If you have any questions,e-mail us at <link><url>mailto:webmaster@spoono.com</url>webmaster@spoono.com</link>. You can see my code that I worked on and made sure worked by right clicking and saving <link><url>list.txt</url>list.txt</link>.
</text>
</tutorial>

