-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexport.php
More file actions
232 lines (184 loc) · 5.83 KB
/
export.php
File metadata and controls
232 lines (184 loc) · 5.83 KB
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
<?php
/*
Programmer : Jay Prakash Jain
Purpose : Class to create for User capabiliyt
Example of how to use the class
include("library/export.php");
$a = new Export();
$a->export_query('read');
$a->export_table('read');
//Funtion to get all user ids below this user
$a->get_sub_usersids();
return true or false
*/
require_once 'excel_reader2.php';
Class Export
{
public $table;
function __construct()
{
// $this->export_table();
// $this->table = 'client_contacts';
// $this->expt();
}
public function downBackup($file)
{
header("Content-Description:File Transfer");;
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($file));
readfile($file);
exit();
}
function export_table($table)
{
$csv_output = "";
//$table = 'client_activities';
//$table = $this->table;
//$file = 'csvexports.csv';
$filess = 'backup/Table_'.$table.date("YmdHis").'.csv';
if($_SESSION['userinfo']['role_id']!=1)
{
$user_query = " Where user_id=".$_SESSION['userinfo']['user_id'];
}
$query = "SELECT * FROM $table".$user_query;
$result = mysql_query($query);
$colCount = mysql_num_fields($result);
$i = $colCount;
for($j = 0; $j < $i; $j++)
{
$rowr = mysql_field_name($result, $j);
$csv_output .= '"' . $rowr . "\",";
}
$csv_output = rtrim($csv_output, ",");
$csv_output .= "\n";
while ($row = mysql_fetch_row($result))
{
for ($j=0;$j<$i-1;$j++)
{
$csv_output .='"'.$row[$j]."\",";
}
$csv_output .='"'.$row[$j]."\"";
$csv_output .= "\n";
}
//readfile('csvexport.csv');
$handle = fopen($filess,'w') or die("can't open file");
fwrite($handle,$csv_output);
fclose($handle);
$this->downBackup($filess);
}
function export_query($query)
{
$csv_output = "";
//$table = 'client_activities';
//$table = $this->table;
//$file = 'csvexports.csv';
$filess = 'backup/Table_'.date("YmdHis").'.csv';
//$query = "SELECT * FROM $table";
$result = mysql_query($query);
$colCount = mysql_num_fields($result);
$i = $colCount;
/*for($j = 0; $j < $i; $j++)
{
$rowr = mysql_field_name($result, $j);
$csv_output .= '"' . $rowr . "\",";
}
$csv_output = rtrim($csv_output, ",");
$csv_output .= "\r\n"; */
while ($row = mysql_fetch_row($result))
{
for ($j=0;$j<$i-1;$j++)
{
$csv_output .='"'.$row[$j]."\",";
}
$csv_output .='"'.$row[$j]."\"";
$csv_output .= "\r\n";
}
//readfile('csvexport.csv');
$handle = fopen($filess,'w') or die("can't open file");
fwrite($handle,$csv_output);
fclose($handle);
$this->downBackup($filess);
}
function Uploadcsv($table_name,$columns,$userid=-1)
{
if(isset($_FILES['csvupload']) && $_FILES['csvupload']['name'] != "")
{
$type = $_FILES["csvupload"]["type"];
//$type = array("application/octet-stream", "text/csv",);
if($type == "application/vnd.ms-excel" || $type = "application/octet-stream")
{
$allowedExts = array("xls");
$extension = end(explode(".", $_FILES["csvupload"]["name"]));
if (in_array($extension, $allowedExts))
{
$target_path = "./backup/";
$target_path1 = $target_path . basename( $_FILES['csvupload']['name']);
if ($_FILES["csvupload"]["error"] > 0)
{
echo "Return Code: " . $_FILES["csvupload"]["error"] . "<br />";
}
else
{
if(move_uploaded_file($_FILES["csvupload"]["tmp_name"],$target_path1))
{
//rename($target_path1,$filename);
$filename = addslashes(realpath('backup')."/".$_FILES['csvupload']['name']);
$filename2 = addslashes(realpath('backup')."/temp.csv");
$data = new Spreadsheet_Excel_Reader($filename);
$this->outputCSV( $data->dumptoarray(),30,$filename2);
/*
$columnnames = array();
if (($handle = fopen($filename, "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$num = count($data);
for ($c=0; $c < $num; $c++)
{
$columnnames[] .= $data[$c];
}
break;
}
fclose($handle);
} */
if(isset($userid) && !empty($userid) && $userid!=-1)
{
$userid_query = 'SET user_id='.$userid;
}
elseif(isset($_SESSION['userinfo']['user_id']))
{
$user_id = $_SESSION['userinfo']['user_id'];
$userid_query = 'SET user_id='.$user_id;
}
$sql ="LOAD DATA LOCAL INFILE '$filename2' INTO TABLE $table_name FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES
TERMINATED BY '\n' ($columns) $userid_query";
$res = mysql_query($sql) or die(mysql_error());
//$affectedRows = $db->getAffectedRows($res);
//echo '<h2>'.$affectedRows .' Record is update <br/><a href="index.php?option=com_adminuserimports&task=update_user">update</a></h2> ';
}
}
}
else
{
echo 'tstst';
}
}else {
echo $type."wrong type";
// echo 'Invalid file type ';
}
}
}
function outputCSV($data,$no_of_columns=30,$csv_name = "myfile.csv" ) {
$outputBuffer = fopen( $csv_name , 'w' );
foreach($data as $val) {
array_splice( $val , $no_of_columns );
fputcsv($outputBuffer, $val);
}
fclose($outputBuffer);
}
}
?>