Reading from Files

You want to read data from a file.

Use file(). It reads everything from a file and places that information into an array. Each array element contains one line from the file, where each line is terminated by a newline (\n or \r\n).

<!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 from Files</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>Reading from a File</h2>
	<form action = "$_SERVER[SCRIPT_NAME]" method="post">
	Filename:
	<input type="text" name="file" size="50" value="data/feedback.txt" />
	<br />
	<input type="submit" name="submit" value="Submit" />
	</form>
HTML;
}

function process_form() {

	$file = "$_POST[file]";

	// check if the file is readable
	if (is_readable($file)) {
		// read the file's contents into an array
		$data = file ("$file");
		// count the number of items in the array
		$number = count($data);
		echo "<p>There are $number lines.</p>";
		foreach ($data as $value) {
			echo "$value<br />";
		}
	}
	else {
		echo "Sorry the file is not readable.";
	}

	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

Use file_get_contents(). It gets the content of the whole file and store it in a string.

<!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>Adding Parameters to a Cookie</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
<!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 from Files</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>Reading from a File</h2>
	<form action = "$_SERVER[SCRIPT_NAME]" method="post">
	Filename:
	<input type="text" name="file" size="50" value="data/feedback.txt" />
	<br />
	<input type="submit" name="submit" value="Submit" />
	</form>
HTML;
}

function process_form() {

	$file = "$_POST[file]";

	// check if the file is readable
	if (is_readable($file)) {
		// read the file's contents into a string
		$data = file_get_contents ("$file");
		echo "<p>$data</p>";
	}
	else {
		echo "Sorry the file is not readable.";
	}

	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

Use readfile(). It reads from a file and writes it to the outpout buffer (sending to the browser). It returns the number of bytes read from 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>Adding Parameters to a Cookie</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
<!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 from Files</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>Reading from a File</h2>
	<form action = "$_SERVER[SCRIPT_NAME]" method="post">
	Filename:
	<input type="text" name="file" size="50" value="data/feedback.txt" />
	<br />
	<input type="submit" name="submit" value="Submit" />
	</form>
HTML;
}

function process_form() {

	$file = "$_POST[file]";

	// check if the file is readable
	if (is_readable($file)) {
		// read the file's contents and sends to the browser
		// use @readfile() to suppress the printing of an error message if an error occured.
		$number_of_bytes = @readfile ("$file");
		echo "<p>Read $number_of_bytes bytes from the file.</p>";
	}
	else {
		echo "Sorry the file is not readable.";
	}

	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