Navigating Directories

To navigate through a directory, you open it, read the content, and close it.

Use opendir(). It creates a pointer, which can be used as a reference to this open directory. Use readdir(). Finally, use closedir().

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>Navigating Directories</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php

if (isset($_POST['submit'])) {
		process_form();
	}
	else {
		display_form();// display form for the first time
	}

function display_form() {
	echo <<<HTML
	<h2>List Directories</h2>
	<form action = "$_SERVER[SCRIPT_NAME]" method="post">
	Enter directory name:
	<input type="text" name="dir" size="50" value="data" />
	<br />
	<input type="submit" name="submit" value="Submit" />
	</form>
HTML;
}

function process_form() {

	$dir = "$_POST[dir]";
	// open the dir
	$dh = opendir($dir);

	// list the directories first
	echo "<p><b>List the contents of $dir directory</b></p>";
	while (false !== ($data=readdir($dh))) {
		echo "$data<br />";
	}

	// reset the pointer
	rewinddir($dh);

	// create a table header
	echo <<<HTML
	<table border>
	<tr>
	<th align="left">File Name</th>
	<th align="left">File Size</th>
	<th align="left">Last Modified</th>
	</tr>
HTML;
	// list the directories
	while ($item = readdir($dh)) {
		if (is_file($item)) {
			// get the file size
			$fs = filesize($item);
			// get the file's modification date
			// F=month, j=day of the month as 1 or 2 digits
			// Y = year as 4 digits
			$fm = date('F j, Y', filemtime($item));
			// display the information
			echo <<<HTML
			<tr>
			<td>$item</td>
			<td>$fs bytes</td>
			<td>$fm</td>
			</tr>
HTML;
		}
	}
	echo "</table>";
	// close the directory
	closedir($dh);

	echo "<p><a href=\"$_SERVER[SCRIPT_NAME]\">
	Try again?</a></p>\n";
	echo "<p><a href=\"example_c.php\">
	Do you want to register?</a></p>\n";
	echo "<p><a href=\"example_r.php\">
	Do you want to login?</a></p>\n";
}

?>
</p>
</body>
</html>

View the effect

Here is another way of navigating by displaying everything without checking for the file.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>Navigating Directories</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php

if (isset($_POST['submit'])) {
		process_form();
	}
	else {
		display_form();// display form for the first time
	}

function display_form() {
	echo <<<HTML
	<h2>List Directories</h2>
	<form action = "$_SERVER[SCRIPT_NAME]" method="post">
	Enter directory name:
	<input type="text" name="dir" size="50" value="data" />
	<br />
	<input type="submit" name="submit" value="Submit" />
	</form>
HTML;
}

function process_form() {

	$dir = "$_POST[dir]";
	// open the dir
	$dh = opendir($dir);

	// list the directories first
	echo "<p><b>List the contents of $dir directory</b></p>";
	while (false !== ($data=readdir($dh))) {
		echo "$data<br />";
	}

	// reset the pointer
	rewinddir($dh);

	// create a table header
	echo <<<HTML
	<table border>
	<tr>
	<th align="left">File Name</th>
	<th align="left">File Size</th>
	<th align="left">Last Modified</th>
	</tr>
HTML;
	// list the directories
	while ($item = readdir($dh)) {
			// get the file size
			$fs = filesize($item);
			// get the file's modification date
			$fm = date('F j, Y', filemtime($item));
			// display the information
			echo <<<HTML
			<tr>
			<td>$item</td>
			<td>$fs bytes</td>
			<td>$fm</td>
			</tr>
HTML;
	}
	echo "</table>";
	// close the directory
	closedir($dh);

	echo "<p><a href=\"$_SERVER[SCRIPT_NAME]\">
	Try again?</a></p>\n";
	echo "<p><a href=\"example_c.php\">
	Do you want to register?</a></p>\n";
	echo "<p><a href=\"example_r.php\">
	Do you want to login?</a></p>\n";
}

?>
</p>
</body>
</html>

View the effect


Directories | Introduction | Creating Directories | Reading Directories | Navigating Directories | Getting File Information
© 2008: Hann So
email: hso@voyager.deanza.edu