...

View Full Version : Write to text file/spreadsheet



ItsMe
06-20-2005, 11:07 AM
Hi,

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,

ItsMe
06-20-2005, 12: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?

delinear
06-20-2005, 12: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");
}

fclose($handle);

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

mrruben5
06-20-2005, 01: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.

http://www.php.net/implode

ItsMe
06-20-2005, 01: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?

mtd
09-03-2005, 04: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:
flock($fp,LOCK_EX);

// Write $savePost to a new line in the file:
fwrite($fp,$savePost."\n");

// Unlock the file so it can be used again:
flock($fp,LOCK_UN);

// Close file:
fclose($fp);

?>


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!
MTD

ItsMe
09-03-2005, 10: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

mtd
09-03-2005, 04: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)

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

mtd
09-03-2005, 04: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.

ItsMe
09-04-2005, 10:22 AM
Hi,

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:
flock($fp,LOCK_UN);

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

Thanks again

ItsMe
09-04-2005, 10: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.

Errors:
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?

Thanks

mtd
09-04-2005, 03: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.

mtd
09-04-2005, 03: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.

ItsMe
09-05-2005, 12: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">
<p>
<input name="Name" type="text" id="Name" value="">
</p>
<p>
<input name="Email" type="text" id="Email" value="">
</p>
<p>
<input name="Phone" type="text" id="Phone" value="">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>

Thanks very much

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

http://www.sjnfreetown.org/mtdurand/WriteToText/form.htm
http://www.sjnfreetown.org/mtdurand/WriteToText/write.php
http://www.sjnfreetown.org/mtdurand/WriteToText/read.php
http://www.sjnfreetown.org/mtdurand/WriteToText/data.txt

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?

MD

mtd
09-05-2005, 05: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.

ItsMe
09-05-2005, 06: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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum