Create CSV data from array
public unparse ( $data = [], $fields = [], $append = false, $is_php = false, $delimiter = null ) : CSV | ||
return | CSV | data (text string) |
} /*if(cellProperties.readOnly) { td.style.opacity = 0.7; }*/ if(!value || value === '') { td.style.background = '#EEE'; } else { td.style.background = ''; } } var data = <?php echo json_encode($csv->unparse($csv->data, $csv->titles, null, null, null, true)); ?> ; var container = $("#example1"); var hand = container.handsontable({ data: data, startRows: data.length, //<?php echo $csv->filelines('../out/test.csv'); ?> , startCols: <?php echo count($csv->titles); ?> , rowHeaders: true, //turn on 1, 2, 3, ...
private function createDBDataSource($filename, $options, $subdir, $file_path) { global $session, $sql; $query = "INSERT INTO datasources (created_at, updated_at, user_id, name, data_path, file_name, `headers`, `lines`)\n\t\t\t\t VALUES(?, NOW(), ?, ?, ?, ?, ?, ?)"; $stmt = $sql->link->prepare($query); if (!$stmt) { die('Invalid query: ' . $sql->link->error); } else { $csv = new parseCSV(); $realUserPath = realpath($file_path); if (filesize($realUserPath) > 0) { $csv->parse($realUserPath, 0, 10000); // At max 10000 lines. $csvDataRows = $csv->unparse($csv->data, $csv->titles, null, null, null, true); } else { $csvDataRows = array(array("")); } $lines = count($csvDataRows) - 1; $headers = json_encode($csv->titles); $subdirSQL = $options['upload_url'] . $subdir; $time = getCurrentDateTime(); $userID = $this->session->get_user_var('id'); $stmt->bind_param('sissssi', $time, $userID, $filename, $subdirSQL, $filename, $headers, $lines); $resultFromExec = $stmt->execute(); if ($resultFromExec) { $affectedRows = $stmt->affected_rows; } /* free result */ $stmt->free_result(); $stmt->close(); } return array("lines" => $lines, "headers" => implode(", ", array_filter($csv->titles))); }
public function readCSV($inData) { $csvData = array('error' => "File Not Found"); $noticeID = $inData['notice_id']; $user_file_dir = $this->session->get_user_var('file_directory'); $basePath = "../user_files/" . $user_file_dir . "/datasources/"; $realBasePath = realpath($basePath); $userPath = $basePath . $inData['subdir'] . $inData['file']; $realUserPath = realpath($userPath); if ($realUserPath === false || strpos($realUserPath, $realBasePath) !== 0) { //Is directory Traversal, bad! $this->outData['data'] = $csvData; } else { //Not directory Traversal. $csv = new parseCSV(); if (filesize($realUserPath) > 0) { $csv->parse($realUserPath, 0, 10000); // At max 10000 lines. $csvDataRows = $csv->unparse($csv->data, $csv->titles, null, null, null, true); } else { $csvDataRows = array(array("")); } $csvData = array("csv_data" => $csvDataRows, "header_count" => count($csv->titles), "headers" => $csv->titles, "row_count" => count($csvDataRows), "csv_errors" => $csv->error_info); $this->outData['data'] = $csvData; } echo json_encode($csvData); }