Last active
December 11, 2015 20:29
-
-
Save keot/4655964 to your computer and use it in GitHub Desktop.
Save Experiment Script Saves any HTTP post data sent to the script to a local CSV file.
Do NOT use this in production. It's hideously insecure.
The directory where the results file is saved requires 'other writeable' (o+w) permissions.
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 | |
/* save_experiment.php | |
* Created by James Mardell <james.mardell@imperial.ac.uk> | |
* Saves any HTTP post data sent to the script to a local CSV file. | |
* Do NOT use this in production. It's hideously insecure. | |
* The directory where the results file is saved requires 'other writeable' (o+w) permissions. | |
*/ | |
// Configuration | |
$results_file = 'data.csv'; | |
function fputcsv($filePointer, $dataArray, $delimiter = ",", $enclosure = "\"") | |
// PHP5 includes this function. Code from <boonerunner@hotmail.com> http://php.net/manual/en/function.fputcsv.php#56827 | |
{ | |
// Build the string | |
$string = ""; | |
// No leading delimiter | |
$writeDelimiter = FALSE; | |
foreach($dataArray as $dataElement) { | |
// Replaces a double quote with two double quotes | |
$dataElement=str_replace("\"", "\"\"", $dataElement); | |
// Adds a delimiter before each field (except the first) | |
if($writeDelimiter) $string .= $delimiter; | |
// Encloses each field with $enclosure and adds it to the string | |
$string .= $enclosure . $dataElement . $enclosure; | |
// Delimiters are used every time except the first. | |
$writeDelimiter = TRUE; | |
} // end foreach($dataArray as $dataElement) | |
// Append new line | |
$string .= "\n"; | |
// Write the string to the file | |
fwrite($filePointer, $string); | |
} | |
// Variables | |
$input = $_POST; | |
// Check to see if the file already exists, if not: write the header | |
if (!is_file($results_file) ) { | |
if (!$output = fopen($results_file, 'a') ) { | |
echo "Cannot open or create file '$results_file' for writing."; | |
exit; | |
} | |
fputcsv($output, array_keys($input) ); | |
fclose($output); | |
} | |
// Write the data | |
if (!$output = fopen($results_file, 'a') ) { | |
echo "Cannot open or create file '$results_file' for writing."; | |
exit; | |
} | |
fputcsv($output, $input); | |
fclose($output); | |
echo "Successfully saved your data."; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment