This repository has been archived by the owner on May 11, 2018. It is now read-only.
/
csvimport.php
372 lines (333 loc) · 9.56 KB
/
csvimport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter CSV Import Class
*
* This library will help import a CSV file into
* an associative array.
*
* This library treats the first row of a CSV file
* as a column header row.
*
*
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author Brad Stinson
*/
class Csvimport {
private $handle = "";
private $filepath = FALSE;
private $column_headers = FALSE;
private $initial_line = 0;
private $delimiter = ",";
private $detect_line_endings = FALSE;
/**
* Function that parses a CSV file and returns results
* as an array.
*
* @access public
* @param filepath string Location of the CSV file
* @param column_headers array Alternate values that will be used for array keys instead of first line of CSV
* @param detect_line_endings boolean When true sets the php INI settings to allow script to detect line endings. Needed for CSV files created on Macs.
* @param initial_line integer Sets the line of the file from which start parsing data.
* @param delimiter string The values delimiter (e.g. ";" or ",").
* @return array
*/
public function get_array($filepath=FALSE, $column_headers=FALSE, $detect_line_endings=FALSE, $initial_line=FALSE, $delimiter=FALSE)
{
// Raise memory limit (for big files)
ini_set('memory_limit', '20M');
// File path
if(! $filepath)
{
$filepath = $this->_get_filepath();
}
else
{
// If filepath provided, set it
$this->_set_filepath($filepath);
}
// If file doesn't exists, return false
if(! file_exists($filepath))
{
return FALSE;
}
// auto detect row endings
if(! $detect_line_endings)
{
$detect_line_endings = $this->_get_detect_line_endings();
}
else
{
// If detect_line_endings provided, set it
$this->_set_detect_line_endings($detect_line_endings);
}
// If true, auto detect row endings
if($detect_line_endings)
{
ini_set("auto_detect_line_endings", TRUE);
}
// Parse from this line on
if(! $initial_line)
{
$initial_line = $this->_get_initial_line();
}
else
{
$this->_set_initial_line($initial_line);
}
// Delimiter
if(! $delimiter)
{
$delimiter = $this->_get_delimiter();
}
else
{
// If delimiter provided, set it
$this->_set_delimiter($delimiter);
}
// Column headers
if(! $column_headers)
{
$column_headers = $this->_get_column_headers();
}
else
{
// If column headers provided, set them
$this->_set_column_headers($column_headers);
}
// Open the CSV for reading
$this->_get_handle();
$row = 0;
while (($data = fgetcsv($this->handle, 0, $this->delimiter)) !== FALSE)
{
if ($data[0] != NULL)
{
if($row < $this->initial_line)
{
$row++;
continue;
}
// If first row, parse for column_headers
if($row == $this->initial_line)
{
// If column_headers already provided, use them
if($this->column_headers)
{
foreach ($this->column_headers as $key => $value)
{
$column_headers[$key] = trim($value, ' \t\n\r\0\x0B'.chr(239).chr(187).chr(191));
}
}
else // Parse first row for column_headers to use
{
foreach ($data as $key => $value)
{
$column_headers[$key] = trim($value, ' \t\n\r\0\x0B'.chr(239).chr(187).chr(191));
}
}
}
else
{
$new_row = $row - $this->initial_line - 1; // needed so that the returned array starts at 0 instead of 1
foreach($column_headers as $key => $value) // assumes there are as many columns as their are title columns
{
$result[$new_row][$value] = utf8_encode(trim($data[$key]));
}
}
unset($data);
$row++;
}
}
$this->_close_csv();
return $result;
}
/**
* Sets the "detect_line_endings" flag
*
* @access private
* @param detect_line_endings bool The flag bit
* @return void
*/
private function _set_detect_line_endings($detect_line_endings)
{
$this->detect_line_endings = $detect_line_endings;
}
/**
* Sets the "detect_line_endings" flag
*
* @access public
* @param detect_line_endings bool The flag bit
* @return void
*/
public function detect_line_endings($detect_line_endings)
{
$this->_set_detect_line_endings($detect_line_endings);
return $this;
}
/**
* Gets the "detect_line_endings" flag
*
* @access private
* @return bool
*/
private function _get_detect_line_endings()
{
return $this->detect_line_endings;
}
/**
* Sets the initial line from which start to parse the file
*
* @access private
* @param initial_line int Start parse from this line
* @return void
*/
private function _set_initial_line($initial_line)
{
return $this->initial_line = $initial_line;
}
/**
* Sets the initial line from which start to parse the file
*
* @access public
* @param initial_line int Start parse from this line
* @return void
*/
public function initial_line($initial_line)
{
$this->_set_initial_line($initial_line);
return $this;
}
/**
* Gets the initial line from which start to parse the file
*
* @access private
* @return int
*/
private function _get_initial_line()
{
return $this->initial_line;
}
/**
* Sets the values delimiter
*
* @access private
* @param initial_line string The values delimiter (eg. "," or ";")
* @return void
*/
private function _set_delimiter($delimiter)
{
$this->delimiter = $delimiter;
}
/**
* Sets the values delimiter
*
* @access public
* @param initial_line string The values delimiter (eg. "," or ";")
* @return void
*/
public function delimiter($delimiter)
{
$this->_set_delimiter($delimiter);
return $this;
}
/**
* Gets the values delimiter
*
* @access private
* @return string
*/
private function _get_delimiter()
{
return $this->delimiter;
}
/**
* Sets the filepath of a given CSV file
*
* @access private
* @param filepath string Location of the CSV file
* @return void
*/
private function _set_filepath($filepath)
{
$this->filepath = $filepath;
}
/**
* Sets the filepath of a given CSV file
*
* @access public
* @param filepath string Location of the CSV file
* @return void
*/
public function filepath($filepath)
{
$this->_set_filepath($filepath);
return $this;
}
/**
* Gets the filepath of a given CSV file
*
* @access private
* @return string
*/
private function _get_filepath()
{
return $this->filepath;
}
/**
* Sets the alternate column headers that will be used when creating the array
*
* @access private
* @param column_headers array Alternate column_headers that will be used instead of first line of CSV
* @return void
*/
private function _set_column_headers($column_headers='')
{
if(is_array($column_headers) && !empty($column_headers))
{
$this->column_headers = $column_headers;
}
}
/**
* Sets the alternate column headers that will be used when creating the array
*
* @access public
* @param column_headers array Alternate column_headers that will be used instead of first line of CSV
* @return void
*/
public function column_headers($column_headers)
{
$this->_set_column_headers($column_headers);
return $this;
}
/**
* Gets the alternate column headers that will be used when creating the array
*
* @access private
* @return mixed
*/
private function _get_column_headers()
{
return $this->column_headers;
}
/**
* Opens the CSV file for parsing
*
* @access private
* @return void
*/
private function _get_handle()
{
$this->handle = fopen($this->filepath, "r");
}
/**
* Closes the CSV file when complete
*
* @access private
* @return array
*/
private function _close_csv()
{
fclose($this->handle);
}
}