Tab-delimited text files w/ PHP
Often in web development, developers are asked to work with many different file formats for a variety of reasons. For instance, most print publications are designed in another program besides Dreamweaver; much of the time it’s Page Maker. To make this publication web friendly, you will need to convert it to Adobe PDF so visitors can view the publication, since Adobe Reader is freely available.
Often when displaying rows of information such as course availability for a school or names and addresses of class alumni from your high school on the web you need a way to dynamically generate the output from a data source. The data source can range from database tables to a simple tab delimited text file. The data source is then read by a server-side script in a web page and the result is regular HTML output to the browser. In this article, we will examine how to create a tab-delimited text file with Excel and then use PHP to read the text file in a web page to display the results.
If you would like to follow this project’s step-by-step development or even try your hand at creating a web page which reads a tab-delimited text file, you’ll find the following project file links helpful.
Create tab-delimited text file
For this article’s purpose, we’ll be using Excel. If you don’t have Excel, just use names.txt which is provided from the project files link. Follow these steps to create your tab-delimited text file:
- From the Desktop, locate the Start button
- Select Programs>Microsoft Office>Microsoft Excel
- The following window will show:

If you look at the finished text file, we need a table which has four (columns) and four (rows). In cells A-1 through A-4 enter the following, in order, starting at A-1:
Ryan
Megan
Brent
Melissa
For cells B-1 through B-4 enter the following in order starting at B-1:
Butler
Butler
Butler
Butler
For cells C-1 through C-4 enter the following in order starting at C-1:
Husband
Wife
Son
Daughter
For cells D-1 through D-4 enter the following in order starting at D-1:
24
26
2
5
Save your spreadsheet.
Before finishing, save the file in Excel’s native format (.xls). (Note: Office 2007 has a different file format.) Follow these steps:
- From the main menu
- Select File>Save
- Save window opens
- Choose an appropriate location and file name
Next, follow these steps to create the tab-delimited file:
- From the main menu
- Select File>Save As
- Save window shows
- In the Save as type drop down list choose Text (Tab delimited):

Left click Save, which prompts this warning:

Left click OK, which prompts this message:

Left click yes. Iif you try to exit Excel, you receive this warning:

This error is a little misleading; it’s referring to any changes which were made to the original spreadsheet that needs to be made to the tab-delimited text file. Since there are no changes, left click No.
Navigate to the directory where you saved the text file, you should see a structure similar to this:

Do note, you can at any time add values to the original spreadsheet and then re-save your text file. If you have not already, close Excel, we’re done with the program.
Create our PHP script
In your text editor, create a new file named names.aspx and insert the following code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html" charset="iso-8859-1"> <title>List of Names</title> </head><body> <?aspx ?> </body> </html>
Between the opening and closing <body> tags, we start a PHP code block.
Reading our text file
Luckily for us, PHP has many built in functions to handle reading data from a tab-delimited text file. The first of these functions is fopen, which is used to open a file. Once we open a file, we’ll assign it to a variable which can be used as a storage location for all data received from the text file. Add the following code to our file:
<?aspx
//create a variable, and assign it the data stream from names.txt
$theFile=fopen("names.txt","r");
?>
Let’s examine the code in greater detail:
- First, we create a variable name theFile by using the dollar ($) sign
- We assign the variable to the fopen function which takes two parameters:
- First, is the name of our tab-delimited file, in our case names.txt
- Second, which mode we want to use. This parameter specifies how we plan to use the data we receive from this text file. Possible options include:
| Access Mode | What it does |
| r | Read-only |
| r+ | Reading-writing |
| w | Write only and create if it does not exist. In addition, the data in the file is erased and you will begin writing data at the beginning of the file. |
| w+ | Reading and writing and creating the file if it does not exist. |
| a | Write only, however, the data in the file is preserved and you begin writing data at the end of the file. |
| a+ | Reading and writing, create the file if it does not exist and place the file position pointer at the end of the file. |
Since no writing is required, we use the read-only option.
We'll continue with checking to see if the file exists on the server next.