61 var $deliminator =
',';
68 var $field_headers=
false;
92 function CSV($filepath=
'')
109 $this->field_headers = &$headers;
124 $this->values = &$values;
139 $this->deliminator = $deliminator;
154 $this->_filename = $filename;
171 $this->filepath = $filepath;
174 $this->_dir = dirname($filepath);
175 if (!file_exists($this->_dir)) mkdir($this->_dir, 0775);
180 if (!file_exists($this->_dir)) {
181 trigger_error(
'CSV directory "'.$this->_dir.
'" does not exist!', E_USER_WARNING);
205 header(
'Content-Type: application/csv');
206 header(
'Content-Disposition: attachment; filename='.$this->_filename.
';');
214 $url_info = parse_url(current_url());
215 $protocol = (isset($url_info[
'scheme'])) ? $url_info[
'scheme'] : NULL;
216 $using_ie6 = strpos($_SERVER[
'HTTP_USER_AGENT'],
'MSIE 6.') !== FALSE;
218 if (!is_null($protocol) && $protocol ==
'https' && $using_ie6) {
219 header(
'Cache-Control: private, max-age=0, must-revalidate');
220 header(
'Pragma: private');
221 header(
'Expires: '.gmdate(
'D, d M Y H:i:s', time()-3600).
' GMT');
245 if (!$this->filepath) {
250 if (!$fp = fopen($this->filepath,
'w')) {
251 trigger_error(
'Unable to open file "'.$this->filepath.
'" for writing', E_USER_WARNING);
256 if ($this->field_headers) {
257 $line = $this->
formatLine($this->field_headers, $this->deliminator);
259 fputs($fp, $line.
"\n");
265 $this->printValues($ordered, $fp);
267 if (isset($fp)) fclose($fp);
284 private function printValues($ordered=
false, $fp=null)
286 foreach ($this->values as $value) {
289 if ($this->field_headers && $ordered) {
292 $line = $this->
formatLine($value, $this->deliminator);
294 if (isset($fp) && $fp !== null) {
295 fputs($fp, $line.
"\n");
318 if ($this->filepath) {
319 if (!$fp = fopen($this->filepath,
'a')) {
320 trigger_error(
'Unable to open file "'.$this->filepath.
'" for writing', E_USER_WARNING);
326 $this->printValues($ordered, $fp);
328 if (isset($fp)) fclose($fp);
340 function import($start_at=1, $lines=0)
343 if (!$this->filepath)
return;
346 if (!is_file($this->filepath))
return;
349 $line_ending_cfg = ini_set(
'auto_detect_line_endings', 1);
351 if (!$fp = fopen($this->filepath,
'r')) {
352 trigger_error(
'Unable to open file "'.$this->filepath.
'" for reading', E_USER_WARNING);
356 $start_at = (int)$start_at;
357 $lines = (int)$lines;
362 while (($tmp = fgetcsv($fp, 65535, $this->deliminator)) != null) {
364 if ($line_count >= $start_at) {
365 $this->values[] = $tmp;
367 if ($lines && ($lines_done >= $lines))
break;
373 if (!$line_ending_cfg) {
374 ini_set(
'auto_detect_line_endings', 0);
392 foreach ($array as $val) {
393 if (is_array($val)) {
394 $line .=
'"'.$this->formatLine($val,$deliminator).
'"'.$deliminator;
396 if (preg_match(
"/[".preg_quote($deliminator,
'/') .
"\"\n\r]/", $val)) {
397 $val =
'"'.str_replace(
'"',
'""', $val).
'"';
399 $line .= $val.$deliminator;
404 return $line = substr($line, 0, (strlen($deliminator) * -1));
420 if (!$this->field_headers)
return '';
421 $line_elements = Array();
423 foreach($this->field_headers as $key => $val) {
425 if (isset($array[$key])) {
426 if (is_array($array[$key])) {
427 $this_val =
'"'.$this->formatLine($array[$key],$deliminator).
'"';
429 $this_val = $array[$key];
432 if (preg_match(
"/[".preg_quote($deliminator,
'/') .
"\"\n\r]/", $this_val)) {
433 $this_val =
'"'.str_replace(
'"',
'""', $this_val).
'"';
437 $line_elements[] = $this_val;
440 return implode($deliminator, $line_elements);
458 if (!$this->filepath)
return;
461 if (!is_file($this->filepath)) {
462 $this->line_count = 0;
466 if (!$fp = fopen($this->filepath,
'r')) {
467 trigger_error(
'Unable to open file "'.$this->filepath.
'" for counting', E_USER_WARNING);
473 while (($tmp = fgetcsv($fp, 65535, $this->deliminator)) != null) {
477 $this->line_count = $line_count - $start_at;
493 if (!$this->filepath)
return true;
496 if (!is_file($this->filepath))
return true;
498 if (!$fp = fopen($this->filepath,
'r')) {
499 trigger_error(
'Unable to open file "'.$this->filepath.
'" for reading', E_USER_WARNING);
503 while (($tmp = fgetcsv($fp, 65535, $this->deliminator)) != null) {
504 if (in_array($string, $tmp))
return false;