Reading Directories

You want to read a directory. A directory can be read by anyone who has read permission on the directory.

Use opendir() to open the directory. Use readdir() to read an entry from a directory handle, given as its argument. It returns the name of the file from the directory. Each file appears in the order in which it is stored by the file system on the disk. It returns either a filename, or false if it fails. If it succeeds, it moves its internal pointer to the next file in the directory, until it reaches the end of the list.

<!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>Reading 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>Login Form</h2>
	<form action = "$_SERVER[SCRIPT_NAME]" method="post">\n
	Username:
	<input type="text" name="username" size="50" value="John" />
	<br />
	Password:
	<input type="password" name="password" size="50" value="John123" />
	<br />
	<input type="submit" name="submit" value="Login" />
	</form>
HTML;
}

function process_form() {

	$loggedin = FALSE; // not currently logged in
	// the data directory was created manually with 0777
	$data_dir = "data";
	// the users.txt file stores the users' information
	$file = "$data_dir/users.txt";
	// open the file for reading
	if ($fh = fopen("$file", 'rb')) {
		// loop through the file by reading 100 bytes or one line
		// whichever comes first with each iteration.
		// The data being read is broken into an array
		// using the | to indicate the separate elements.
		while ($line = fgetcsv($fh, 1000, "|")) {
			// check the file data against the submitted data
			// $line[0]=username, $line[1]=password
			// $line[2]=directory
			echo "<p>Display the values to see:</p>";
			foreach ($line as $key=>$value) {
				echo "$key=>$value<br />";
			}
			// the password was encrypted, it needs to be checked
			// against an encrypted version of the submitted password
			if (($line[0] == $_POST[username]) AND ($line[1] == crypt($_POST[password], $line[1]))) {
				// Correct user name and password
				$loggedin = TRUE;
				echo "<p>You are now logged in.</p>";
				// open the directory that contains the users info
				$dh = opendir("$data_dir");
				// list all files in the directory
				echo "<p><b>List the contents of $data_dir directory</b></p>";
				while (false !== ($data=readdir($dh))) {
					echo "$data<br />";
				}
				// close the user directory
				closedir($dh);
				// stop looking through the file
				break;
			}

		}
		// close the file
		fclose($fh);
		// print a message
		if ($loggedin) {
			echo "<p><b>List the contents of $line[2] directory</b></p>";
			// check if it's a directory
			if (is_dir("$line[2]")) {
			// open the directory for the user
				$dh = opendir("$line[2]");
				// list all files in the directory
				while (false !== ($data=readdir($dh))) {
					echo "$data<br />";
				}
				// close the user directory
				closedir($dh);
			}
			else {
				echo "<p>The directory is empty.</p>";
			}
		}
		else {
			echo "<p>Incorrect username and/or password.</p>";
		}
	}
	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";
}

?>
</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