Created
June 26, 2012 13:06
-
-
Save alexsegura/2995697 to your computer and use it in GitHub Desktop.
Simple HTML form to create CSV files with PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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