$url = parse_url(get_bloginfo('url')); $fp = fopen( $_SERVER['DOCUMENT_ROOT'] . $url['path'] . '/wp-content/uploads/export/answers.csv', 'w+'); if ($fp != false && count($results) > 0) { fputcsv($fp, array_keys($results[0]), ";");// Add the fields name foreach($results as $result) { fputcsv($fp, $result, ";"); } fclose($fp); }