web
web

Reputation: 1

PHP fputcsv() in not working properly

I want to put the out -put of HTML form in a csv file having header names: "NAME", "EMAIL", "WEB". But the following PHP code is not working. Could any body help in this?

<?php
$fp = fopen("my_file.xls", "a");

$name = $_POST['name'];
$email = $_POST['email'];
$web   = $_POST['web'];

if (!$fp) {
    print("Could not open to file");
    exit;
}

fput($fp, "NAME", "EMAIL",'WEB");
fputcsv($fp, $name, $email, $web);

print("Thanks you ");

fclose($fp);
?>

I have tried by replacing my_file.xls with my-file.csv. After submission of HTML form, the output is blank CSV file.

Expected : Comma separated values from the form with Headers.

Please help me locate the issue.

Upvotes: 0

Views: 4244

Answers (1)

Manse
Manse

Reputation: 38147

Try this :

$fp = fopen("my_file.xls", "a");

$name = $_POST['name'];
$email = $_POST['email'];
$web   = $_POST['web'];

if (!$fp) {
    print("Could not open to file");
    exit;
}
fputs($fp, "NAME,EMAIL,WEB");        
fputcsv($fp,array($name, $email, $web));

print("Thanks you ");

fclose($fp);

fputcsv expects an array of values as a second parameter and fput should be fputs and expects a string as a second parameter

Upvotes: 2

Related Questions