Skip to content

Instantly share code, notes, and snippets.

@alexsegura
Created June 26, 2012 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexsegura/2995697 to your computer and use it in GitHub Desktop.
Save alexsegura/2995697 to your computer and use it in GitHub Desktop.
Simple HTML form to create CSV files with PHP
<?php
if (isset($_POST['rows'])) {
$h = tmpfile();
foreach ($_POST['rows'] as $row) {
fputcsv($h, array_values($row));
}
rewind($h);
$csv = '';
while (($row = fgets($h)) !== false) {
$csv .= $row;
}
fclose($h);
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo $csv;
exit;
}
?>
<html>
<head></head>
<body>
<form method="post">
<input type="text" name="rows[0][]" />
<input type="text" name="rows[0][]" />
<input type="text" name="rows[0][]" />
<hr />
<input type="text" name="rows[1][]" />
<input type="text" name="rows[1][]" />
<input type="text" name="rows[1][]" />
<hr />
<input type="submit" />
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment