/
readCsv.php
133 lines (121 loc) · 3.54 KB
/
readCsv.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
<?php
session_start();
require __DIR__ . '/vendor/autoload.php';
use League\Csv\Reader;
use SimpleCrud\SimpleCrud;
$pdo = new PDO('mysql:host=www.db4free.net;port=3306;dbname=fultonfile', 'fultonfile', 'qweasd123qweasd123');
define("RESULT_FILE", "https://fultonfile-agusnurwanto.rhcloud.com");
// echo "<pre>". print_r($_FILES,1) ."</pre>";
// die();
$res = array( "error" => 0 );
if((!empty($_POST['action']) && $_POST['action']=="read_csv")
|| (!empty($_FILES) && $_FILES['async-upload']["type"]=="text/csv")){
$pathResultReadCSV = __DIR__. "/uploads/resultReadCSV.json";
$file = array();
$cekFile = true;
if(!file_exists($pathResultReadCSV)){
$file = handle_upload();
$cekFile = false;
}
if(empty($file["error"])){
if(empty($cekFile)){
$csv = Reader::createFromPath($file["file"]);
$csvData = $csv->setOffset(1)->fetchAll();
$keys = [];
foreach ($csvData as $k => $v) {
if(!empty($v[1])){
$key = getKey($v[1]);
$keys[] = $key;
}else if(!empty($v[0])){
$key = getKey($v[0]);
$keys[] = $key;
}
}
}else{
$data = file_get_contents($pathResultReadCSV);
$keys = json_decode($data);
}
$allData = readResultFile();
foreach ($allData as $k => $v) {
$key = getKey($v);
$cek = array_search($key, $keys);
if($cek>=0){
unset($keys[$cek]);
}
}
$res['msg'] = [];
$res['msg_err'] = [];
$res['key'] = count($allData);
foreach ($keys as $key => $value) {
// if(strlen($value)<13){
// $res['msg_err'][] = $value;
// }else{
$res['msg'][] = $value;
// }
}
if(!empty($keys)){
file_put_contents($pathResultReadCSV, json_encode($keys));
}else{
unlink($pathResultReadCSV);
}
}else{
$res = $file;
}
}else{
$res["error"] = 1;
$res["msg"] = $_FILES;
}
function readResultFile(){
global $pdo;
$db = new SimpleCrud($pdo);
$allData = $db->fultonfile->select()->all();
return $allData->parselNumber;
}
function getKey($string){
$number = preg_replace( '/\ /', '+', $string );
$number = preg_replace( '/-/', '', $number );
return $number;
// return $string;
}
function handle_upload(){
$res = array("error" => 0);
// fix error upload;
// $res["file"] = __DIR__.'/Fulton.csv';
// return $res;
try {
if (!isset($_FILES['async-upload']['error']) || is_array($_FILES['async-upload']['error'])) {
throw new RuntimeException('Invalid parameters.');
}
switch ($_FILES['async-upload']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
throw new RuntimeException('No file sent.');
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException('Exceeded filesize limit.');
default:
throw new RuntimeException('Unknown errors.');
}
if ($_FILES['async-upload']['size'] > 100000000) {
throw new RuntimeException('Exceeded filesize limit.');
}
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search(
$finfo->file($_FILES['async-upload']['tmp_name']),
array( 'csv' => 'text/plain' ),
true
)) {
throw new RuntimeException('Invalid file format.'.$finfo->file($_FILES['async-upload']['tmp_name']));
}
if (!move_uploaded_file( $_FILES['async-upload']['tmp_name'], './uploads/fulton.csv')) {
throw new RuntimeException('Failed to move uploaded file.');
}
$res["file"] = __DIR__.'/uploads/fulton.csv';
} catch (RuntimeException $e) {
$res["error"] = 1;
$res["msg"] = $e->getMessage();
}
return $res;
}
die(json_encode($res));