View Full Version : Write to text file/spreadsheet

06-20-2005, 12:07 PM

Im trying to make a PHP script that writes a few fields, say name and email, to either a text, spreadsheet or simelar file.

Can anyone help me with this?

Thanks in advance,

06-20-2005, 01:05 PM
I know this is possible to write to a single file once, but i want to write to a file many times, so each time it is submitted it adds the information on the end, not overwrites whats already there.

Is this possible?

06-20-2005, 01:44 PM
You can append information to the end of existing information in a file by using the "a" argument in your fopen statement, eg fopen($file, "a"). Here's a little script that should give you the idea:

$file = 'your_filename.txt';
$content = "Some content";

if (!$handle = fopen($file, 'a')) {
exit("Could not open file: $file");

if (fwrite($handle, $content) === FALSE) {
exit("Cannot write to file: $file");


06-20-2005, 01:58 PM
Thanks, i will look at that.

06-20-2005, 02:08 PM
if you want to make csv files, you can use the implode function to put strings together on one line seperated by anything you want.


06-20-2005, 02:53 PM
Thanks for the help, but im still a bit unsure. I will explain exactly what i want it to do.

There is a form in which users can enter their detail (name, email, website etc.) and this writes it to a file on the server.

There is then a page which reads the data file and displays each persons details in a table.

Can anyone give me some help on achieving this?

09-03-2005, 05:20 AM
To save the info, try:


// Define file to save to:
$fileLocation = '/path/to/file.txt';

// Implode the $_POST array,
// Save each value in format "Value0 || Value1 || Value2" etc:
$savePost = implode(' || ', $_POST);

// Open file (defined above),
// Set action to 'a' for 'append' - won't overwrite existing file contents:
$fp = fopen($fileLocation,'a') or die('Could not open file.');

// Lock file temporarily so nobody can access it while it's being written to:

// Write $savePost to a new line in the file:

// Unlock the file so it can be used again:

// Close file:


Then to read it in the future, try:


// Define file to read:
$fileLocation = '/path/to/file.txt';

// Read the file into an array by line:
$fileContents = file($fileLocation);

//Start table:
echo '<table><tr>';
echo '<td>NAME</td>';
echo '<td>EMAIL</td>';
echo '<td>PHONE</td>';
echo '</tr>';

// Loop thru the file by line:
foreach($fileContents as $line) {
// Trim newline \n and other whitespace:
$line = rtrim($line);
// Explode Values into an array:
$values = explode(' || ', $line);
// Print the results:
echo '<tr>';
for($n=0; $n<count($values); $n++) echo '<td>'.$values[$n].'</td>';
echo '</tr>';

// End table:
echo '</table>';


I haven't tested this so I may have made a silly mistake/typo, but the general idea is there.

To recap, let's say a form has values 'Name', 'Email', 'Phone'

The first part reads the Posted form, saves the values to $savePost like this: Name || Email || Phone
Then it writes that to a new line of the file you're using to save all this info

The second part reads the file by line, and for each line
explodes the Name || Email || Phone string into an array.
Then it prints the values to a table:

Name 1 Email 1 Phone 1
Name 2 Email 2 Phone 2
Name 3 Email 3 Phone 3

Hope this helps!

09-03-2005, 11:48 AM
That code looks great, i will look into it further and test it later.

So all i do is add a form page that submits the variables Name, Email and Phone to the first PHP script you have shown me?

Thanks very much

09-03-2005, 05:23 PM
If all goes as planned, that is correct. submit the form to whatever php page has the first bit of code. FYI, you may need to alter the permissions (777) of the file you are writing to, but try it normal first because it often depends on server permissions.

Another thing I thought of. $_POST will likely include the value for the Submit button (i.e. $button = true). If you don't want that to be printed to the table, try adding this to the first part of the code between $fileLocation and $savePost:

// Remove last form element (submit button):
$lastElement = array_pop($_POST);
The array_pop removes the last element from the $_POST array, so assuming the Submit button is the last form element (usually the case), it will be removed from the $_POST and will not be saved in $savePost. Depending on how you are implementing the table in context of the big picture, this may be important or not so in the least... your call.

Hope it works for you. If you need any more help, let me know. (I'm not exactly a pro, so the practice helps)

09-03-2005, 05:29 PM
Thanks very much, looks great. will implement it later and let you know how it goes.

09-03-2005, 05:30 PM
Also know that you can use any inputs that you want. They don't have to be limited to or include NAME, EMAIL, PHONE. The only part that is hard-coded is at the beginning of the second script, where it sets up the table headers (echo '<td>NAME</td>'). You would just have to edit those headers for whatever inputs you have, in the order that they are posted from the form. Or, you could just do away with those headers, but I personally find it easier to always label the info in my data tables.

09-04-2005, 11:22 AM

I have just tested this script and all seems to be great apart from i get a parse error on the following line of code on the write script:

// Unlock the file so it can be used again:

I cannot see anything wrong with this code, can you?

Thanks again

09-04-2005, 11:40 AM
Hi again,

I have solved the parse error problem, i found a ; missing on the line above. now i am getting the following 2 errors after i submit my form to the write page.

Warning: Bad arguments to implode() in write.php on line 16

Warning: Wrong parameter count for fwrite() in write.php on line 26

Any ideas?


09-04-2005, 04:02 PM
I edited my original code to include the following updates:

- Added missing ; that you mentioned
- fixed fwrite() error - the problem was that I forgot to reference $fp. It used to read fwrite($savePost."\n"); and now (correctly) reads fwrite($fp,$savePost."\n");

I still have to look into the Bad arguments error, I'll let you know if I find anything. Like I said, I didn't test the code (I actually wrote it write here on the forum), so I do appologize for those errors.

09-04-2005, 04:35 PM
I tested the code in DW and on my server, and I don't get that implode error. I did, however, re-write the code in DW just to be safe...maybe I missed something before? Anyway, I attached the two pages that I used that seemed to work, see if you have the same trouble with those.

09-05-2005, 01:25 PM
Hi again,

i have copied the code out of the files you attached and i now get another error along with the bad arguments. The errors are:

Warning: The argument needs to be an array in write.php on line 11

Warning: Bad arguments to implode() in write.php on line 12

I am using your code for the read and write part and the following basic form to submit to it. as far as i can see everything is ok.

<form name="form1" method="post" action="write.php">
<input name="Name" type="text" id="Name" value="">
<input name="Email" type="text" id="Email" value="">
<input name="Phone" type="text" id="Phone" value="">
<input type="submit" name="Submit" value="Submit">

Thanks very much

09-05-2005, 06:18 PM
I couldn't see a problem. It all works great on my server. Try it out here:


FYI, write.php and read.php are identical to the test1.txt and test2.txt that I uploaded in my last post, just renamed.

I put in one entry already to test it, so you'll see one there under my name...

I'm stumped tho, because there are really no issues with implode() that should cause variation from server to server... Anyone else have any idea where that error might be originating and why?


09-05-2005, 06:32 PM
To start at the beginning, try adding a print_r($_POST); at the beginning of the first script, write.php
Make sure all the values are being passed correctly in the $_POST array - it is the only array used in that file, so I would think that must be where the problem is originating?

For that matter, for every variable defined, run a print_r() after it is defined to make sure it's what's expected. Before the implode() there are only two variables plus the global $_POST, so the problem would have to be somewhere there.

This is all I can think of so far.

09-05-2005, 07:09 PM
I cannot see why this is happening either. I am not very advanced at php currently so i cant see how to fix this problem.

If you could have a look, the files are located at www.andrewthomas.org.uk/write_to_file and let me know if you see any problems.

I have also added in the print_r($_POST); bit at the beginning of write.php as you suggessted.

Many Thanks