Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP to text file 1

Status
Not open for further replies.

Djbell

IS-IT--Management
Apr 22, 2002
175
GB
Hi All

I have a contacts Form with the following fields.

Name
Email
Comments

I would like when the button post is clicked this writes the information to a text file, I have been told that this is easy in PHP but I have no idea how to do this..

Any help is appreciated.

Regards

Djbell
 
follow this code through

Code:
$string['name'] = trim($_POST['name']);
$string['email']= trim($_POST['email']);
$string['comments'] = trim($_POST['comments']);

$string_to_write = assembleString($string);

if (@constant('PHP_EOL') === null){
 $eol = "\r\n";
} else {
 $eol = PHP_EOL;
}

//this is the file to which the data will be written
//ensure that the php server process has WRITE access to this file
$filename = "c:/textfile.txt";
$fh = fopen($filename, "wbt") or
  die("Cannot open file for writing");
fwrite($fh, $string_to_write . $eol);
fclose($fh);

function assembleString($string){
 //this function cleans the string up, escapes nasty characters and then surrounds it in quotation marks, finally adding a comma after each element

 if (!is_array($string)){
  return '"'.(addslashes(trim($string))).'"';
 }
 $return = "";
 foreach ($string as $s){
  $return .= '"'.(addslashes(trim($s))).'",';
 }
 return rtrim($return ",");
}

note - i have not tested this so there may be parse errors.
 
Hi

Thanks for the reply..

I get a parse error on line 32

Regards

Djbell
 
Sorry cant read, line 30.

return rtrim($return ",");

Regards

Djbell
 
as i said ...

there is a comma missing in that line. change it to :
Code:
return rtrim($return, ",");
 
Brilliant

Thanks for the help..

Regards

Djbell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top