/
uploads.php
96 lines (71 loc) · 2.12 KB
/
uploads.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
<?php
require_once("sitewide/opener.php");
?>
<title>Make N Share</title>
<?php
require_once("sitewide/header.php");
?>
<?php
if (!empty($_FILES["fileToUpload"])) {
// Where the file is going to be placed
$userid = $_SESSION['u_id'];
$target_path = './uploads/'.$userid.'/';
if (!file_exists($target_path)) {
mkdir($target_path, 0777, true);
}
$uploadOk = 1;
if (file_exists($target_path. basename( $_FILES['fileToUpload']['name']))) {
echo "File already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 5000000) {
echo "STL is too large.";
$uploadOk = 0;
}
// Allow certain file formats
$FileType = strtolower(pathinfo($target_path. basename( $_FILES['fileToUpload']['name']),PATHINFO_EXTENSION));
if($FileType != "stl") {
echo "Only stl files are allowed.";
$uploadOk = 0;
}
if($uploadOk != 0) {
$target_path = $target_path. basename( $_FILES['fileToUpload']['name']);
if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_path))
{
echo "The file ". basename( $_FILES['fileToUpload']['name']).
" has been uploaded";
$filename = basename( $_FILES['fileToUpload']['name']);
$query = "INSERT INTO uploads
SET user_id ='$userid',
file_name ='$filename',
file_path = '$target_path'";
mysqli_query($mysqli, $query);
}
else
{
echo "There was an error uploading the file, please try again!";
}
}
}
?>
<div class="container">
<div id="header">
<img src="img/gray-logo.png" id="logo">
<a href="home.php" id="upload-button">BACK</a>
</div>
<div id="content">
<form method="post" enctype="multipart/form-data">
<div class="upload-btn-wrapper">
<button class="btn">CHOOSE STL FILE</button>
<input type="file" name="fileToUpload" id="fileToUpload">
</div>
<br><br><input type="submit" value="UPLOAD FILE" name="submit" class="btn">
</form>
</div>
<div id="footer">
</div>
</div>
<?php
require_once("sitewide/footer.php");
?>