Writing to Files

You want to write to files.

Use fopen() to open the file, fwrite() to write, and fclose() to close the file.

You should store the files in a directory (folder) located outside the Web directory if posible to avoid people accessing it from the Web. Set the permissions of the folder so that everyone can write to, read from, and execute or search (0777 in Unix).

<!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>Writing to 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>Feedback Form</h2>
	<form action = "$_SERVER[SCRIPT_NAME]" method="post">
	Filename:
	<input type="text" name="file" size="50" value="data/feedback.txt" />
	<br />
	Comments:
	<textarea name="comments" rows="5" cols=""50">here are my comments.</textarea>
	<br />
	<input type="submit" name="submit" value="Submit" />
	</form>
HTML;
}

function process_form() {

	$file = "$_POST[file]";
	$data = stripslashes($_POST['comments']);

	// Open the file for writing. Create a file if it doesn't exist.
	// Overwrite any existing content.
	// the flag b is used so that the file is opned in a binary safe mode.
	if ($fh = fopen("$file", 'wb')) {
		// write the data
		fwrite($fh, "$data\n");
		fclose($fh);
		echo "<p>Your feedback has been stored.</p>";
	}
	else {
		echo "<p>Your feedback could not be stored.</p>";
	}

	echo "<p><a href=\"$_SERVER[SCRIPT_NAME]\">Try again?</a></p>\n";
	echo "<p><a href=\"example_r.php\">
	Do you want to read from the file?</a></p>\n";
}
?>
</p>
</body>
</html>

View the effect

Here you want to append the data to the file by using the mode a+.

<!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>Writing to 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>Feedback Form</h2>
	<form action = "$_SERVER[SCRIPT_NAME]" method="post">
	Filename:
	<input type="text" name="file" size="50" value="data/feedback.txt" />
	<br />
	Comments:
	<textarea name="comments" rows="5" cols=""50">here are my comments.</textarea>
	<br />
	<input type="submit" name="submit" value="Submit" />
	</form>
HTML;
}

function process_form() {

	$file = "$_POST[file]";
	$data = stripslashes($_POST['comments']);

	// Open the file for writing. Create a file if it doesn't exist.
	// Append the data to the end of the file.
	// the flag b is used so that the file is opned in a binary safe mode.
	if ($fh = fopen("$file", 'ab')) {
		// write the data
		fwrite($fh, "$data\n");
		fclose($fh);
		echo "<p>Your feedback has been stored.</p>";
	}
	else {
		echo "<p>Your feedback could not be stored.</p>";
	}

	echo "<p><a href=\"$_SERVER[SCRIPT_NAME]\">Try again?</a></p>\n";
	echo "<p><a href=\"example_r.php\">
	Do you want to read from 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