Reading Files with CSV

You want to read a file containing data in a delineated format (commonly using CSV, comma-separated values).

Use fgetcsv(). It breaks the string into parts, using the marked delimiter, and returns an array.

<!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 Files with CSV</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
	$file = "data/users.txt"; // file that stores the names and passwords
	// 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
			echo "<p>Display the values to see:</p>";
			foreach ($line as $key=>$value) {
				echo "$key=>$value<br />";
			}
			if (($line[0] == $_POST[username]) AND ($line[1] == $_POST[password])) {
				// Correct user name and password
				$loggedin = TRUE;
				// stop looking through the file
				break;
			}

		}
		// close the file
		fclose($fh);
		// print a message
		if ($loggedin) {
			echo "<p>You are now logged in.</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_l.php\">
	Do you want to write to the file?</a></p>\n";
}
?>
</p>
</body>
</html>

View the effect

Here is another way of processing the variable-length text fields.

<!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 Files with CSV</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
	$file = "data/users.txt"; // file that stores the names and passwords
	$delim = "|";
	// open the file for reading
	$fh = fopen("$file", 'r') or die("can't open: $php_errormsg");
	while (!feof($fh)) {
		$s = rtrim(fgets($fh));
		$line = explode($delim, $s);
			echo "<p>Display the values to see:</p>";
			foreach ($line as $key=>$value) {
				echo "$key=>$value<br />";
			}
			if (($line[0] == $_POST[username]) AND ($line[1] == $_POST[password])) {
				// Correct user name and password
				$loggedin = TRUE;
				// stop looking through the file
				break;
			}
	}

		// close the file
		fclose($fh);
		// print a message
		if ($loggedin) {
			echo "<p>You are now logged in.</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_l.php\">
	Do you want to write to the file?</a></p>\n";
}
?>
</p>
</body>
</html>

View the effect


Files | Introduction | File Permissions | Creating or Opening a File | Writing to Files | Locking Files | Reading from Files | Reading Files Incrementally | Reading Files with CSV | File Upload
© 2008: Hann So
email: hso@voyager.deanza.edu