Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Parse Gravity Forms CSV Upload to PHP Array WordPress

  • SOLVED

Been trying at this for awhile, and this is a seemingly simple function, but I am stuck... trying to parse a csv uploaded through a Gravity Forms form into a useable php array. Whenever I try the conventional php functions like fgetcsv() and fopen() etc. the array is empty, so I am clearly missing a step somewhere, but I am not sure what it is. Maybe it needs to be utf8 encoded before handling it?... help is appreciated here

Once I get it into the array, I am fine, just need the csv broken into an array by rows with the column headers as the keys.

The below attempt is using the most popular function from the str_getcsv page on php.net:

add_action("gform_post_submission_66", "csv_to_event",10,2);
function csv_to_event($entry, $form){

$csv = $entry[1]; //this returns a url string to the uploaded csv
$array = $fields = array(); $i = 0;
$handle = @fopen($csv, "r");
if ($handle) {
while (($row = fgetcsv($handle, 4096)) !== false) {
if (empty($fields)) {
$fields = $row;
continue;
}
foreach ($row as $k=>$value) {
$array[$i][$k] = $value;
}
$i++;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
var_dump($array);

}

Answers (2)

2013-12-31

Fahad Murtaza answers:

Can you share your CSV file. Just the headings with a few lines ?


Kyle comments:

Sure, I attached it here