Creating Directories

You want to make directories.

Use mkdir('directory_name', permissions). The permissions are 0777 by default. On Windows servers,the permissions are ignored.

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

function process_form() {

	$problem = FALSE; // No problems so far
	// the data directory was created manually with 0777
	$data_dir = "data";
	// the users.txt file stores the users' information
	$file = "$data_dir/users.txt";

	// check for each value
	if (empty($_POST['username'])) {
		$problem = TRUE;
		echo "<p>Please enter a username.</p>";
	}

	if (empty($_POST['password1'])) {
		$problem = TRUE;
		echo "<p>Please enter a password.</p>";
	}

	if ($_POST['password1'] != $_POST['password2']) {
		$problem = TRUE;
		echo "<p>Your password did not match your confirmed password.</p>";
	}

	if (!$problem) {
		// open the file
		if ($fh = fopen ($file, 'a')) {
			// create a directory for the user based on the time
			// the user registered and a random value.
			// This guarantees that the directory is unique and has a valid name.
			$dir = time().rand(0, 4596);
			// create the data to be written (on Windows add \r\n)
			// use the crypt() to encrypt the password.
			$data = $_POST['username']."|".crypt($_POST['password1'])."|".$dir."\n";
			// write the data and close the file
			fwrite ($fh, $data);
			fclose ($fh);
			// close the directory in the data directory
			mkdir ("$data_dir/$dir");
			// print a message
			echo "<p>Thank you for registering.</p>";
		}
		else {
			// couldn't write to the file
			echo "<p>You couldn't be registered due to a system error.</p>";
		}
	}

	echo "<p><a href=\"$_SERVER[SCRIPT_NAME]\">
	Try again?</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