PHP: Hot Pages: A Poll: Save data to a file

  1. A Poll
  2. Arrays

What we want to do is save the data to a simple file. Here’s a very simple vote counter class that only does that. Add it to fields.phpi:

class VoteCounter extends FormField {

protected $ballotFolder = '/home/USERNAME/ballots/';

protected $filePath;

public function __construct($fieldName) {


$this->filePath = $this->ballotFolder . $fieldName . '.txt';


//append the vote, if it exists, to the specified file in ballotFolder

public function save() {

if ($this->submitted && $this->value !== '') {

$choice = $this->value;

$ballots = fopen($this->filePath, 'a');

fwrite($ballots, "$choice\n");





This class takes one parameter when you create a VoteCounter object from it: the field name, which it uses to know what form field to look for and what file to store the ballots in. It has one method other than the __construct method, and that method saves the results to the file. It uses three “f” functions to do this; the “f” stands for “file”, and these are “fopen” to open a file for use, “fwrite” to write to the opened file, and “fclose” to close the file and let another web page or visitor use it.

The “!==” is the “not equals” equivalen to the triple equal. If $this->value is anything other than a string of text and an empty string of text, it will match here and allow saving.

Create the folder

The first thing to do is set up a folder that we can allow the web page to write to. By default, web pages are not allowed to write to most places on the server. This is for very good security reasons: if web pages could write more files, it would be easier for them to hack the system.

Commonly, you’ll make a data folder off of your account’s home directory and outside of your web site, so that no one can access the data files directly. Create a ballots folder in your home directory if you’re using Mac OS X, Linux, or something like that:

mkdir ~/ballots

chmod o+wr ~/ballots

This creates the ballots folder and then makes it writable and readable by “other”. The “o+wr” means “add write and read access to other”. “Other” is the world, basically, and almost always includes your web pages. Sometimes, you may have to use “g+wr” or “u+wr” if your web pages are in the same “group” as your account, or if your web pages use your own account’s privileges.

You don’t have to worry too much about this, but don’t go around giving maximum privileges to every file. Privileges are designed to help keep your system—and your data—secure both from prying and tampering.

Save the first ballot

Add this to the top of poll.php:



$vote = new VoteCounter('character');



Whenever someone visits the page, this will save any submission they make in a file called “character.txt” in the ballots folder that you’ve specified in the $ballotFolder property.

Load the page, make a choice, and look in your ballots directory. There should be a file called “character.txt” and it should contain the choice you made in the pull-down menu. Make another choice, and that should be reflected in a new line in character.txt.

Hide the form

After they vote, of course, we want to both thank them for voting and hide the form from them.

<?php IF ($vote->answered()):?>

<p>Thank you for voting!</p>

<?php ELSE:?>

<form method="post" action="poll.php">


Please choose your favorite imaginary imaginary character:

<select name="character">

<option value="">Choose:</option>

<option value="rabbit">The White Rabbit</option>

<option value="scarecrow">The Scarecrow</option>

<option value="tinman">The Tin Man</option>

<option value="neo">Neo</option>


<input type="submit" value="Submit your answer" />



<?php ENDIF;?>

This tells them “thank you for voting” rather than showing the form, if $vote->answered() returns true. The answered method is on the FormField class; since VoteCounter extends FormField, $vote can use the answered method, too.

  1. A Poll
  2. Arrays