-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.php
58 lines (40 loc) · 1.96 KB
/
import.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
<?php
// Load the database configuration file
include_once 'includes/config.php';
if(isset($_POST['importSubmit'])){
// Allowed mime types
$csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
// Validate whether selected file is a CSV file
if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'], $csvMimes)){
// If the file is uploaded
if(is_uploaded_file($_FILES['file']['tmp_name'])){
// Open uploaded CSV file with read-only mode
$csvFile = fopen($_FILES['file']['tmp_name'], 'r');
// Skip the first line
fgetcsv($csvFile);
$truncate= $db->query("TRUNCATE TABLE stocktbl;");
$db->query($insert);
// Parse data from CSV file line by line
while(($line = fgetcsv($csvFile)) !== FALSE){
// Get row data
$date = $line[1];
$stock_name = $line[2];
$price = $line[3];
// Insert member data in the database
$insert= $db->query("INSERT INTO stocktbl (date, stock_name, price, created, modified) VALUES (STR_TO_DATE('".$date."','%d-%m-%Y'), '".$stock_name."', '".$price."', NOW(), NOW())");
$db->query($insert);
}
// Close opened CSV file
fclose($csvFile);
$qstring = '?status=succ';
// Redirect to the listing page
header("Location: stock.php".$qstring);
}else{
$qstring = '?status=err';
// Redirect to the listing page
header("Location: index.php".$qstring);
}
}else{
$qstring = '?status=invalid_file';
}
}