-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfiles 2.php
More file actions
203 lines (178 loc) · 5.51 KB
/
files 2.php
File metadata and controls
203 lines (178 loc) · 5.51 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
<?php
/******File Handling*******/
$key_arr = array("username", "password", "type");
$key_arr_progress = array("username", "type", "date", "numAttempted","numCorrect", "numWon");
/*Write $data to $filename*/
function save_data($filename, $data){
$str = join(" ", $data)."\n";
//$myfile = fopen("DB/$filename", "w") or die("Failed to create files");
/*Append data to $filename*/
/*More fopen modes on page 149*/
$myfile = fopen($filename, "a") or die("Failed to create files");
fwrite($myfile, $str) or die("Could not write to file");
fclose($myfile);
}
function update_file($file, $data){
//override
$myfile = fopen($file, "w") or die("Failed to create files");
$str = "";
foreach ($data as $key => $value) {
$str .= join(" ", $value);
}
fwrite($myfile, $str) or die("Could not write to file");
fclose($myfile);
}
/***Reading from a file: fgets**/
function get_user_info($filename){
global $key_arr;
$myfile = fopen($filename, "r") or die("File does not exist");
/*could use fread()*/
while($line=fgets($myfile)){
//Convert to array by " "
$res = explode(" ", $line);
$new_res = [];
//Replace keys in $res
for($i = 0; $i<count($key_arr); $i++){
$new_res[$key_arr[$i]] = $res[$i];
}
$info_arr[$res[0]] = $new_res;
//Destory local variable $new_res
unset($new_res);
unset($res);
}
fclose($myfile);
return $info_arr;
}
function add_new_progress_info($filename,$username,$userType){
require_once "config.php";
$date = date("Y-m-d");
// echo "<br>Our username: ".$username;
// echo "<br>Our date: ".$date;
$myfile = fopen($filename, "a+") or die("File does not exist");
$full_file_content = file_get_contents($filename);
$entry_content = explode(";", $full_file_content);
// echo "<br>entry_content: ";
// print_r($entry_content);
$entryExists = false;
$count = count($entry_content);
// echo "<br>count: ".$count;
for ($entry=0; $entry < count($entry_content)-1; $entry++) {
$single_entry = explode(" ", $entry_content[$entry]);
#check if first element of name is newline (ascii=10). if so, chop it off!
$ascii = ord($single_entry[0]);
if ($ascii == 10){
$this_username = substr($single_entry[0], 1);
}
else{
$this_username = $single_entry[0];
}
#compare username and date
$user_comp = strcmp($this_username,$username);
if($user_comp == 0){
$date_comp = strcmp($single_entry[2],$date);
if($date_comp == 0){
$entryExists = true;
break;
}
}
}
if ($entryExists == false){
$array = array($username, $userType, $date, 0, 0, 0);
$str = implode(" ", $array);
$str = $str.";";
fwrite($myfile, $str);
}
fclose($myfile);
}
function increment_num_probs_attempted($filename,$username){
require_once "config.php";
$date = date("Y-m-d");
$myfile = fopen($filename, "a+") or die("File does not exist");
$content = file_get_contents($filename);
$entries_content = explode(";", $content);
$return_content = "";
for($i=0; $i < count($entries_content)-1; $i++) {
$single_entry = explode(" ", $entries_content[$i]);
#check if first element of name is newline (ascii=10). if so, chop it off!
$ascii = ord($single_entry[0]);
if ($ascii == 10){
$this_username = substr($single_entry[0], 1);
}
else{
$this_username = $single_entry[0];
}
if($this_username == $username){
if($single_entry[2] == $date){
$single_entry[3]++;
}
}
$return_content = $return_content.implode(" ", $single_entry).";";
}
file_put_contents($filename, $return_content);
fclose($myfile);
}
function increment_num_probs_correct($filename,$username){
require_once "config.php";
$date = date("Y-m-d");
$myfile = fopen($filename, "a+") or die("File does not exist");
$content = file_get_contents($filename);
$entries_content = explode(";", $content);
$return_content = "";
for($i=0; $i < count($entries_content)-1; $i++) {
$single_entry = explode(" ", $entries_content[$i]);
#check if first element of name is newline (ascii=10). if so, chop it off!
$ascii = ord($single_entry[0]);
if ($ascii == 10){
$this_username = substr($single_entry[0], 1);
}
else{
$this_username = $single_entry[0];
}
if($this_username == $username){
if($single_entry[2] == $date){
$single_entry[4]++;
}
}
$return_content = $return_content.implode(" ", $single_entry).";";
}
file_put_contents($filename, $return_content);
fclose($myfile);
}
function increment_num_games_won($filename,$username){
require_once "config.php";
$date = date("Y-m-d");
$myfile = fopen($filename, "a+") or die("File does not exist");
$content = file_get_contents($filename);
$entries_content = explode(";", $content);
$return_content = "";
for($i=0; $i < count($entries_content)-1; $i++) {
$single_entry = explode(" ", $entries_content[$i]);
#check if first element of name is newline (ascii=10). if so, chop it off!
$ascii = ord($single_entry[0]);
if ($ascii == 10){
$this_username = substr($single_entry[0], 1);
}
else{
$this_username = $single_entry[0];
}
if($this_username == $username){
if($single_entry[2] == $date){
$single_entry[5]++;
}
}
$return_content = $return_content.implode(" ", $single_entry).";";
}
file_put_contents($filename, $return_content);
fclose($myfile);
}
/*****Copy a file*****/
/*$file2_name = "DB/useer_copy.txt";
if(!copy($file_name, $file2_name)){
echo "Could not copy file";
}else{
echo "Copy $file_name to $file2_name";
}*/
/*Question: How should we update use.txt file;*/
/***Delete a file***/
//echo unlink($file_name)? "Delete file $file_name" : "Could not delete file $file_name";
?>