2017-01-27 16:05:02 +01:00
|
|
|
#!/usr/bin/php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once(__DIR__."/functions.inc.php");
|
|
|
|
|
|
|
|
$csv=getenv("csv");
|
|
|
|
$range=parse_colnum(getenv("columns"));
|
2017-01-30 10:31:37 +01:00
|
|
|
$rows=parse_colnum(getenv("rows"));
|
|
|
|
$rowsf=array_flip($rows);
|
2017-01-27 16:05:02 +01:00
|
|
|
$rangef=array_flip($range);
|
|
|
|
$head=getenv("header");
|
|
|
|
$delim=getenv("delimiter");
|
|
|
|
|
|
|
|
$c=fopen($csv,"r");
|
|
|
|
|
|
|
|
if ($head) {
|
|
|
|
$header=fgetcsv($c,false,$delim);
|
|
|
|
} else {
|
|
|
|
$header=Array();
|
|
|
|
$i=0;
|
|
|
|
foreach ($range as $c) {
|
|
|
|
$header[$i]=sprintf("FIELD%d",$i);
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
json_init();
|
|
|
|
$line=0;
|
|
|
|
$last=end($range);
|
2017-01-30 10:31:37 +01:00
|
|
|
$lastrow=max($rows);
|
2017-01-27 16:05:02 +01:00
|
|
|
|
|
|
|
while ($row=fgetcsv($c,false,$delim)) {
|
|
|
|
$line++;
|
2017-01-30 10:31:37 +01:00
|
|
|
if (!array_key_exists($line,$rowsf)) continue;
|
2017-01-27 16:05:02 +01:00
|
|
|
json_row();
|
|
|
|
json_column("ROW","$line");
|
|
|
|
foreach ($range as $num) {
|
2017-01-30 10:31:37 +01:00
|
|
|
$col=$num-1;
|
|
|
|
json_column("COLUMN$col",$header[$col]);
|
|
|
|
json_column("VALUE$col",addslashes($row[$col]),$last==$num);
|
2017-01-27 16:05:02 +01:00
|
|
|
}
|
2017-01-30 10:31:37 +01:00
|
|
|
json_row_end(feof($c)||$line==$lastrow);
|
2017-01-27 16:05:02 +01:00
|
|
|
}
|
|
|
|
json_end();
|
|
|
|
|
|
|
|
fclose($c);
|
|
|
|
|