PHP File Manager
Editing File: Import.php
<?php /* * Copyright (C) Wayne Purton-Smith - All Rights Reserved * Unauthorized copying of this file or removing this paragraph, via any medium is strictly prohibited * Proprietary and confidential * Written by Wayne Purton-Smith <waynepurtonsmith@hotmail.co.uk> February 2014 */ ini_set('auto_detect_line_endings', 1); class Import extends CI_Model { public function validateCSV($csv = '') { $temp = tmpfile(); fwrite($temp, $csv); fseek($temp, 0); $valid = (bool) fgetcsv($temp); fclose($temp); return $valid; } public function parseCSV($csv_contents = '') { $data = array(); $temp = tmpfile(); fwrite($temp, $csv_contents); fseek($temp, 0); $header_keys = fgetcsv($temp); while(!feof($temp)) { if(($row = fgetcsv($temp)) !== false) { $data[] = array_combine($header_keys, $row); } } fclose($temp); // this removes the file return $data; } public function convertKeys($data = array()) { if(count($data)) { $new_data = array(); $header_keys = array_map(function($key_name) { return preg_replace('/\s+/', '_', trim(strtolower($key_name))); }, array_keys(reset($data))); foreach($data as $i => $row) { $new_data[] = array_combine($header_keys, array_values($row)); } return $new_data; } return array(); } public function toCSV($data = array(), $delimiter = ',', $enclosure = '"') { $contents = ''; $handle = fopen('php://temp', 'r+'); foreach($data as $line) { fputcsv($handle, $line, $delimiter, $enclosure); } rewind($handle); while(!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle); return $contents; } }
Cancel