Search code examples
phpcsvfputcsv

convert $_POST values to CSV


Updated Situation:

I have managed to create a registration form that outputs the data to a new CSV file for use into a separate database. However the code that sends the attachment doesn't work, but the system stores the file into a separate folder (I must stress this is while I am testing).

My question is, can I both create the CSV file and send it via email? If so, what do I need to do?

I am also aware that there are security risks: One suggestion has been to store the CSV files outside to root directory. is this foolproof and if not, could anyone make any recommendations?

My code is:

<?php

$_POST['password'] = md5($_POST['password']);

$headers=array_keys($_POST);

$file = "csv/" . $_POST['username'].date('Ymdhis').".csv"; //filename

$file = fopen($file, 'a');
fputcsv($file, $headers);
fputcsv($file, $_POST);
fclose($file);

require_once('email/PHPMailerAutoload.php');

$m = new PHPMailer;

$m->isSMTP();
$m->SMTPAuth = true;
$m->SMTPDebug = 0;

$m->Host = 'mymail.com';
$m->Username = 'me@mymail.com';
$m->Password = 'mypass';
$m->SMTPSecure = 'SSL';
$m->Port = 26;

$m->From = 'me@mymail.com';
$m->FromName = 'My name';
$m->addReplyTo('me@mymail.com', 'Reply Address'); 
$m->AddAddress('me@mymail.com', 'My name');

$m->isHTML(true);

$m->addAttachment('csv/data.csv', 'data.csv');

$m->Subject = 'New feed ';
$m->Body = '<p>This is an email just sent from our website</p><p><strong>Please import into your database.</strong></p>';
$m->AltBody = 'This is the body.  Please import into the database';

if($m->send()) {
echo '<h1>Thank you</h1>  <p>We have received your Registration and will send you confirmation details shortly.</p>';

} else {
  echo $m->ErrorInfo;
}

I also have a checkbox field that needs to be written with the commas separating the values, for example 1,2,3. How can I write this?

Any assistance/advice will as always be gratefully received.

Many thanks in advance.


Solution

  • $_POST['id']="101";                               //example data
    $_POST['username']="kraysak";                     //example data
    $_POST['password']="grumpycat";                   //example data
    $_POST['usergroup']="admin";                      //example data
    $_POST['lastaccess']="14-10-2014 00:01";          //example data, 
    
    $_POST['password'] = md5($_POST['password']);
    
    $headers=array_keys($_POST);
    
    $file = $_POST['username'].date('Ymdhis').".csv"; //filename
    
    $file = fopen($file, 'a');
    fputcsv($file, $headers ); //write headers (key of the $_POST array (id,username,password,etc)
    fputcsv($file, $_POST );
    fclose($file);
    

    this create a file named kraysak20141014010253.csv with the next information:

    id,username,password,usergroup,lastaccess
    101,kraysak,5557981401e83c1963412f19c7487965,amdin,"14-10-2014 00:01"
    

    is hard for me to explain, (my english isn't good enought) but fputcsv funtion write the content of an array in the file, and $_POST is an array... so, you dont need to create new variables, you only need to use md5() function in one variable.