Uploading data from a form to a flat file


#1

I don’t know how to make the data from the form write to a flat file. I have the HTML for the form already made and some of the coding for the processing of the data, I just don’t know how to make the data go into the file. The code I’ve created so far is below, although it’s probably wrong. I’d appreciate any help that you can give me.

[code]<?php
$name=@$_POST[‘name’];
$email=@$_POST[‘email’];
$website=@$_POST[‘website’];
$comments=@$_POST[‘comments’];

$values=“Name: $namern”;
$values=“Email: <a href=/”$email/">$emailrn";
$values=“Website: <a href=/”$website/">$websitern";
$values=“Comments: $commentsrn”;
?>[/code]

Thanks in advance


#2

Hi,

What you need are fopen(), fwrite() and fclose(). These 3 functions will do what you need.

Something like:

    $data = $name. "|" .$email. "|" .$website. "|" .$comments. "|n";
    $fp = fopen(/home/serverpath/files/data.log", "ab") or die("ERROR!! Cannot create 'data.log' file.")

    if ($fp != false)

    {

      fwrite($fp, $data);
      fclose($fp);

    }

The data.log file will be created automatically the first time you execute your form. Make sure that the directory that holds the file (in this example ‘files’) is writeable on your server. ie, chmod to 777.

Or you can just echo the value of ‘values’: $data = $values; However, note that your $values variable should be concatenated.

$values="Name: $namern"; 
$values.="Email: <a href=/"$email/">$email</a>rn"; 
$values.="Website: <a href=/"$website/">$website</a>rn"; 
$values.="Comments: $commentsrn";

Hope that helps.


#3

At http://www.codewalkers.com there is a tutorial called “Working with Text Files” in the tutorials/basics section. You might wish to check it out.


#4

Thanks everyone.

That site’s really good :D.