-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_file.php
715 lines (556 loc) · 21.2 KB
/
upload_file.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
<?php
session_start();
include "user_password.php";
if ((!isset($_SESSION["admin_ps"])) || $_SESSION["admin_ps"] != $user_password) {
unset($_POST);
unset($_FILES);
unset($_SESSION["admin_ps"]);
header("Location: ./login.php");
}
$error = "";
$sucess = "";
if (isset($_POST['delete']) && $_POST['delete'] == "true") {
$_SESSION['admin_del'] = "admin";
// echo "delete is set";
header("Location:./select_file.php");
}
if (isset($_POST['logout']) && $_POST['logout'] == "true") {
unset($_POST);
unset($_FILES);
unset($_SESSION["admin_ps"]);
unset($_SESSION["admin_del"]);
session_destroy();
header("Location:./login.php");
}
function seo_friendly_url($string)
{
$string = trim($string);
$len = strlen($string);
$i = 0;
for ($i = 0; $i < $len; $i++) {
if ($string[$i] == '#' || $string[$i] == '$' || $string[$i] == '%' || $string[$i] == '=' || $string[$i] == '"' || $string[$i] == "'" || $string[$i] == '@' || $string[$i] == '!' || $string[$i] == '^' || $string[$i] == '&' || $string[$i] == '*' || $string[$i] == '+' || $string[$i] == '`' || $string[$i] == '~' || $string[$i] == '?' || $string[$i] == ',' || $string[$i] == '<' || $string[$i] == '>' || $string[$i] == '?' || $string[$i] == ':' || $string[$i] == ';' || $string[$i] == '{' || $string[$i] == '[' || $string[$i] == ']' || $string[$i] == '}' || $string[$i] == '|') {
$string[$i] = '-';
}
}
return (($string));
}
function split_with_extension_name($str, &$real_name, &$exten)
{
$len = strlen($str);
while ($len > 0 && $str[$len - 1] != ".") {
$exten = $str[$len - 1] . $exten;
$len--;
}
$len--;
while ($len > 0) {
$real_name = $str[$len - 1] . $real_name;
$len--;
}
$exten = strtolower($exten);
return 0;
}
// echo '<pre>';
// print_r($_POST);
// printf("<br> count is %d ", count($_FILES['up_file'], 1));
// echo "<br> size of file = " . sizeof($_FILES) . " post - " . sizeof($_POST);
// echo '</pre>';
if (isset($_FILES['up_file']) && $_FILES['up_file']['size'] && isset($_POST['submit']) && $_POST['submit'] == "true") {
// echo '<pre>';
// print_r($_FILES);
// echo "error is<br>";
// echo $_FILES['up_file']['error'];
// echo "error is<br>";
// echo print_r(error_get_last());
// echo '</pre>';
$no_of_file = count($_FILES["up_file"]["name"]);
// echo "<br> Totoal no of ifle is: " . $no_of_file;
$no_of_file_uploaded = 0;
$file_extension_arr["php"]= 1;
$file_extension_arr["js"]= 1;
include "password.php";
for ($i = 0; $i < $no_of_file; $i++) {
$f_name = seo_friendly_url($_FILES["up_file"]["name"][$i]);
$f_temp_name = $_FILES["up_file"]["tmp_name"][$i];
$sub = trim($_POST['subject']);
$unit = trim($_POST['unit']);
$cat = trim($_POST['cat']);
if ($sub == "0" || $unit == "0" || $cat == "0") {
$error = "All Fields are Required";
break;
} else if (empty(trim($_FILES['up_file']['name'][$i]))) {
$error = "Please upload a valid file";
break;
} else if ($_FILES['up_file']['size'][$i] > 40000000 || $_FILES['up_file']['size'][$i] <= 0) {
$error .= "File name : " . $_FILES['up_file']['name'][$i] . " <br>";
} else {
$file_name = "";
$path_name = "./upload/" . $sub . "/unit-" . $unit . "/" . $cat;
$real_name = "";
$exten = "";
split_with_extension_name($f_name, $real_name, $exten);
// echo "<br> realname =$real_name";
// echo" <br> fanem = ".$f_name;
// echo "<br> exten = " . $exten;
// echo "<br>";
//check if file file extension is valid
// echo "arr is set " .$file_extension_arr[$exten] ;
// echo "<br> is set return ".isset($file_extension_arr[$exten])."<br>";
if (isset($file_extension_arr[$exten]) ) {
// echo "file extesn set " . $file_extension_arr[$exten];
$error .= "Not able to upload File name = '$f_name'<br>";
} else {
// echo "file exten not set ";
// $conn = new mysqli("localhost", "root", "", "my_db") or die("Not able to connect");
$table_name = "topic";
$sql = "SElECT count(*) FROM " . $table_name;
$result = $conn->query($sql);
//create table topic if not exists
// echo '<pre>'.print_r($result).'</pre>';
// echo '<br>connection error is:'.$conn->error."end";
if ($result == "") {
// echo "<br>creating table table not existt ";
$sql = "CREATE table $table_name ( topic_name varchar(100) ,unit varchar(10))";
$result = $conn->query($sql);
// if ($result != "") {
// echo "<br>created table " . $table_name . " successfulll<br>";
// } else {
// echo "<br>not created table<br>";
// }
}
$table_name = $sub . $unit;
//create table sub+unit if not exist
$sql = "SElECT count(*) FROM " . $table_name;
$result = $conn->query($sql);
// if ($result == "" && $conn->error == "Table 'my_db." . $table_name . "' doesn't exist") {
if ($result == "") {
$sql = "CREATE table $table_name ( cat varchar(100) , path_name varchar(300))";
$result = $conn->query($sql);
// if ($result != "") {
// // echo "created table " . $table_name . " successfulll<br>";
// } else {
// // echo "not created table<br>";
// }
}
//insert into table topic
$sql = "SELECT * FROM topic where (topic_name='$sub' and unit='$unit')";
$result = $conn->query($sql);
// echo '<pre>';
// echo "<br><br>$sql";
// print_r($result);
// echo '</pre>';
if ($result->num_rows === 0) {
$sql = "INSERT into topic values( '$sub','$unit')";
$result = $conn->query($sql);
// echo "<br>result of insert new in topic ";
// print($result);
}
// insert into sub
$sql = "SELECT * FROM $table_name where (cat='" . $cat . "' AND path_name='" . $path_name . "/$real_name.$exten" . "')";
$result = $conn->query($sql);
$file_name = "$real_name.$exten";
if ($result->num_rows != 0) {
$count = 0;
while ($result->num_rows != 0) {
$count++;
$sql = "SELECT * FROM $table_name where (cat='" . $cat . "' AND path_name='" . $path_name . "/$real_name(" . $count . ").$exten')";
// echo "<br>$sql";
$result = $conn->query($sql);
// echo "<br> count = " . $count;
// echo "--";
// print_r($result);
// echo "--";
}
$file_name = $real_name . "(" . $count . ").$exten";
// echo "<br>----->$file_name";
}
$sql = "INSERT into $table_name values( '" . $cat . "','" . $path_name . "/$file_name')";
// echo "<br>$sql";
$result = $conn->query($sql);
// if ($result != "") {
// echo "<br>result of insert new in topic ";
// }
// print_r($result);
if (file_exists($path_name) == false || is_dir($path_name) == false) {
mkdir($path_name, 0777, true);
// if (is_dir($path_name)) {
// // echo "this is directoryu ";
// } else {
// echo "<br>this is not directory";
// }
// echo "<br> created new path ";
}
// echo "<br>file name si: " . $file_name;
if (move_uploaded_file($f_temp_name, $path_name . "/" . $file_name)) {
$sucess .= "File name : '$file_name' <br>";;
$no_of_file_uploaded++;
// unset($_FILES);
} else {
$error .= "Not able to upload File name = $file_name<br>";
$error .= "because " . $conn->error;
}
}
}
}
$conn->close();
// echo "result is";
// print_r($result);
// echo "<br>conect error = " . $conn->error;
if ($sucess != "") {
$sucess = "Uploaded $no_of_file_uploaded Files Successfully<hr>" . $sucess;
}
if ($error != "") {
if ($error != "All Fields are Required" && $error != "Please upload a valid file") {
$error = "Error uploading " . ($no_of_file - $no_of_file_uploaded) . " Files <hr> " . $error;
}
}
}
// else {
// echo "<h1>not set nay files </h1>";
// }
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="favicon.png" type="image/x-icon">
<title>Upload Document</title>
</head>
<style>
body {
/* background-color: brown; */
background-color: rgb(234, 230, 241);
font-size: 20px;
margin: 0px;
padding: 0px;
font-family: "Helvetica";
/* color:white; */
}
input {
background-color: rgb(114, 14, 14);
color: white;
}
#form_boundary {
width: 65%;
/* width:500px; */
margin: auto;
/* background-color:green; */
/* margin:100px; */
padding-bottom: 50px;
padding-top: 20px;
word-break: break-all;
}
.box {
border: rgb(212, 199, 199) solid 1px;
border-radius: 5px;
background-color: white;
margin: 20px;
padding: 12px;
/* height: 100px; */
}
header {
padding: 20px;
margin-top: 0px;
border-radius: 5px;
border-top-left-radius: 0px;
border-top-right-radius: 0px;
background-color: #fff;
/* font-weight: 700; */
font-size: 30px;
/* text-align: center; */
}
p {
font-size: 15px;
/* font-weight: bo; */
}
select {
width: 50%;
padding: 2px;
/* border-radius:2px; */
/* font-size: 10px; */
/* background-color: rgb(223, 223, 247); */
}
#head {
/* padding-top:10px; */
background-color: rgb(15, 15, 168);
/* margin-top:38px; */
/* font-size:20px; */
padding: 8px 0px 0px 0px;
}
button {
border: 0px solid transparent;
border-radius: 4px;
/* width:40%; */
padding: 8px 40px;
font-weight: 500px;
color: white;
background-color: rgb(45, 45, 197);
}
button:hover {
background-color: rgb(86, 86, 219);
}
#sub_but,
#logout_but,
#del_but {
margin: 0px 20px;
}
#logout_box {
display: inline-block;
}
input {
font-size: 20px;
/* color:black; */
}
.file_name {
/* background-color: blue; */
background-color: rgb(212, 212, 230);
padding: 2px 10px;
}
#message {
padding: 23px;
}
#conform_box_content {
width: 500px;
/* border:2px solid yellow; */
margin: auto;
}
#conform_box {
word-break: keep-all;
border: 1px solid black;
position: fixed;
width: 500px;
/* z-index: 1;; */
padding: 20px 10px;
top: -200px;
color: white;
background-color: rgba(0, 0, 0, 0.8);
transition: 0.5s;
margin: auto;
}
@media screen and (min-width:1200px) {
#main_box {
width: 1124px;
margin: auto;
}
}
@media screen and (max-width:900px) {
#form_boundary {
width: 95%;
/* background-color: blue; */
}
select {
width: 80%;
padding: 2px;
/* border-radius:2px; */
/* font-size: 10px; */
/* background-color: rgb(223, 223, 247); */
}
header {
font-size: 25px;
}
#button_box {
text-align: center;
}
#sub_but,
#logout_but,
#del_but {
width: 92%;
margin: 10px;
/* position: relative; */
}
#inp_but {
width: 100%;
}
}
@media screen and (max-width:600px) {
#form_boundary {
width: 97%;
/* background-color: blue; */
}
select {
width: 90%;
padding: 2px;
/* border-radius:2px; */
/* font-size: 10px; */
/* background-color: rgb(223, 223, 247); */
}
header {
font-size: 20px;
}
#button_box {
text-align: center;
}
#sub_but,
#logout_but,
#del_but {
width: 92%;
margin: 10px;
/* position: relative; */
}
#inp_but {
width: 100%;
}
}
@media screen and (max-width:400px) {
#form_boundary {
width: 97%;
/* background-color: blue; */
}
select {
width: 96%;
padding: 2px;
/* border-radius:2px; */
/* font-size: 10px; */
/* background-color: rgb(223, 223, 247); */
}
header {
font-size: 15px;
}
#button_box {
text-align: center;
}
#sub_but,
#logout_but,
#del_but {
width: 83%;
margin: 10px;
/* position: relative; */
}
#inp_but {
width: 100%;
}
}
</style>
<body>
<div id="conform_box_content">
<div id="conform_box">
<p id="conform_mess"> Uploading...</p>
</div>
</div>
<div id="main_box">
<div id="form_boundary">
<form id="form" action="" method="POST" enctype="multipart/form-data">
<div id="head" class="box">
<header> Upload Documents
<hr>
</header>
</div>
<div id="message" class="box" <?php
if ($error != "") {
echo ' > <p style="color:red ;font-size:20px;">' . $error . '</p>';
}
if ($error == "" && $sucess != "") {
echo ">";
}
if ($sucess != "") {
echo ' <p style="color:green ;font-size:20px;">' . $sucess . '</p>';
} else if ($error == "" && $sucess == "") {
echo 'style="display:none;">';
}
?> </div> <div class="box">
<p>Topic *</p>
<select name="subject" id="subject">
<option selected hidden value="0"> Select </option>
<option value="ds">Data Structure and Algorithms</option>
<option value="de">Digital Electronics</option>
<option value="ppl">Principles of Programming Languages</option>
<option value="math">Mathamatics</option>
<option value="os">Operating System</option>
</select>
</div>
<div class="box">
<p>Unit *</p>
<select name="unit" id="unit">
<option value="0" hidden selected>Select </option>
<option value="1">UNIT-1</option>
<option value="2">UNIT-2</option>
<option value="3">UNIT-3</option>
<option value="4">UNIT-4</option>
<option value="5">UNIT-5</option>
<option value="6">PRACTICAL</option>
</select>
</div>
<div class="box">
<p>Category * </p>
<select name="cat" id="cat">
<option value="0" hidden> Select </option>
<option value="note">Notes</option>
<option value="book">Books</option>
<option value="qp">Question </option>
<option value="vd">Videos </option>
</select>
</div>
<div class="box">
<p>Select Files *</p>
<br>
<button type="button" id="inp_but" onclick="test_fun()"> Add Files</button>
<input type="file" name="up_file[]" id="up_file_but" multiple onchange="show_name()" hidden>
<div id="file_name">
<p class="file_name">No File Selected</p>
</div>
</div>
<div id="button_box">
<button id="sub_but" type="submit" name="submit" value="true"> Submit</button>
<button id="del_but" type="submit" name="delete" value="true"> Delete</button>
<button id="logout_but" type="submit" name="logout" value="true"> Logout</button>
</div>
</form>
</div>
</div>
<script>
var inp_but = document.getElementById("inp_but");
var upfile_but = document.getElementById("up_file_but");
var file_name = document.getElementById("file_name");
var message = document.getElementById("message");
var sub_but = document.getElementById("sub_but");
var del_but = document.getElementById("del_but");
var conform_box = document.getElementById("conform_box");
var conform_mess = document.getElementById("conform_mess");
var form = document.getElementById("form");
function test_fun() {
upfile_but.click();
// file_name.textContent = "No File Selected<hr>No File Selected";
file_name.innerHTML = "<p class='file_name' > No Files Selected <p>";
}
del_but.addEventListener("click", function() {
// console.log(up_file_but.files.length);
upfile_but.files.value = null;
// console.log(up_file_but.files.length);
});
sub_but.addEventListener("click", function() {
document.body.style.cursor = "progress";
conform_box.style.top = "20%";
form.style.display = "none";
})
// conform_box.style.top = "20%";
function show_name() {
if (up_file_but.files != null) {
var count = up_file_but.files.length;
var str_name = "";
var total_size = 0;
for (var i = 0; i < count; i++) {
if (up_file_but.files.item(i) != null && up_file_but.files.item(i).name != null) {
if (up_file_but.files.item(i).size > 40000000) {
str_name += "<p class='file_name' style='background-color:red; color:white'>" + up_file_but.files.item(i).name + " -- Too large file <p>";
} else if (up_file_but.files.item(i).size <= 0) {
str_name += "<p class='file_name' style='background-color:red ;color:white'>" + up_file_but.files.item(i).name + " --Too small file <p>";
} else {
str_name += "<p class='file_name' >" + up_file_but.files.item(i).name + " <p>";
}
total_size += up_file_but.files.item(i).size;
}
}
if (total_size > 40000000) {
str_name += "<p class='file_name' style='background-color:red ;color:white' >Warning: Total File size : " + total_size + "Bytes. Sum of file size must be smaller than 40000000Bytes or 40MB for Succesfull upload. <p>";
}
file_name.innerHTML = str_name;
conform_mess.textContent = " Uploading " + count + " Files. Please wait ....";
// console.log(upfile_but.value);
}
}
setTimeout(() => {
message.style.display = "none";
}, 30000);
</script>
</body>
</html>