-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_subject.php
78 lines (58 loc) · 1.58 KB
/
delete_subject.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
<?php
function test_input($data, $con)
{
$data = trim($data);
$data = htmlspecialchars($data);
$data = ltrim($data, '\\');
$data = ltrim($data, '/');
$data = mysqli_real_escape_string($con, $data);
return $data;
}
include 'db_config.php';
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
$dept = test_input($_POST["dept"], $con);
$year = test_input($_POST["year"], $con);
$subject = test_input($_POST["subject"], $con);
$link;
if($dept!="" and $year!="" and $subject!="") {
$link = "uploads\\" . $dept . "\\" . $year . "\\" . $subject . "\\";
} else {
echo "Error: Either of parameters are missing";
exit;
}
if( !is_dir($link) ) {
echo "Error: Directory does not exist";
exit;
}
$sql = "SELECT subjects from curr_subjects WHERE dept = '$dept' AND year = '$year'";
$res = mysqli_query($con,$sql);
$row = mysqli_fetch_array($res);
if($row != null) {
$result = explode(',', $row[0]);
$index = array_search($subject, $result);
unset($result[$index]);
$result = implode(",", $result);
$sql = "UPDATE curr_subjects SET subjects = '$result' WHERE dept = '$dept' AND year = '$year'";
$res = mysqli_query($con,$sql);
if($res)
{
if(delTree($link)) {
echo "Success";
} else {
echo "Error: Directory could not be deleted";
}
} else {
echo "Database update failure";
}
//echo $result;
} else {
echo "Error: dept year not correct";
}
function delTree($dir) {
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
?>