-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport.php
163 lines (121 loc) · 3.83 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Rutgers Stock Prediction Analyzer</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Le styles -->
<link href="view/css/bootstrap.css" rel="stylesheet">
<link href="view/css/bootstrap-responsive.css" rel="stylesheet">
<link href="view/css/Gal.css" rel="stylesheet">
</head>
<body class="container">
<div class="wrapper">
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="index.php">RSPA</a>
<div class="nav-collapse">
<ul class="nav">
<li class="">
<a href="index.php">Home</a>
</li>
<li class="active">
<a href="import.php">Import CSV</a>
</li>
<li class="">
<a href="view/about.html">About</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="page-header">
<h1>
Rutgers Stock Prediction Analyzer <small>By: Gal Cohen
</small>
</h1>
<h3> CSV File Upload</h3>
</div>
<form class="well" enctype="multipart/form-data" action="import.php" method="POST">
Enter PIN:
<input name="pin" type="text" /> (1234)</br>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<input name="userfile" type="file" /> </br>
<input type="submit" value="Send File" />
</form>
<!-- <a href="index.php">[Homepage]</a> <p> -->
</div>
</body>
</html>
<?php
include("config.php");
include("model.php");
if (isset($_POST["pin"]) ) {
$pin = $_POST["pin"];
$retval = shell_exec("sudo chmod 777 ~csuser/cs336-final/");
$retval = shell_exec("sudo chmod 777 ~csuser/cs336-final/data/");
if ($pin == 1234){
$uploaddir = 'data/';
$destination = $uploaddir . basename($_FILES['userfile']['name']);
if ($_FILES["userfile"]["type"] == "text/csv"){
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $destination)) {
echo "File is valid, and was successfully uploaded.\n";
if (readCSVFile($destination)){
echo "New recommendations successfully inserted to database.";
}else{
}
} else {
echo "<h4> Error: Could not upload file. </h4>";
}
}else {
echo "<h4> Error: Incorrect file type. Must be a CSV file. </h4>";
}
//echo $destination;
//print_r($_FILES);
}else{
echo "<h4> Error: Incorrect pin entered. </h4>";
}
}else{
echo "<h4> Error: A field is missing. </h4>";
}
function readCSVFile($filename){
global $localDB;
//mysql_select_db($sql_database, $localDB);
$row = 1;
//$queryString = "INSERT INTO recommendations VALUES"
if (($handle = fopen($filename, "r")) !== false) {
while (($data = fgetcsv($handle, 800, ",", '"')) !== false) {
if ($row == 1){
$row++;
continue;
}
$num = count($data);
if (!isset($data[0]) || !isset($data[1]) || !isset($data[2]) ||!isset($data[3])){
echo "Error in CSV file, could not insert into database.";
fclose($handle);
return false;
}
echo $data[0]." ".$data[1]." ".$data[2]." ".$data[3]."</br>" ;
//$queryString .= "( ".$data[0].", ".$data[1].", ".$data[2].", ".$data[3]." )"
if ($data[2] == "positive"){
$data[2] = 1;
}else if ($data[2] = "negative") {
$data[2] = -1;
}else if ($data[2] == "hold"){
$data[2] = 0;
}
mysql_query("REPLACE INTO recommendations VALUES ('$data[0]', '$data[1]', '$data[2]', '$data[3]')", $localDB);
}
fclose($handle);
return true;
}
return false;
}
?>