-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupload.php
More file actions
231 lines (209 loc) · 8.02 KB
/
Copy pathupload.php
File metadata and controls
231 lines (209 loc) · 8.02 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
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
<?php $_SESSION['MSG'] = "Please login first."; ?>
<?php include "php/checkLogin.php"; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<title> MUlody Project </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src='js/document_ready.js'></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">
<link href="css/fileupload.css" rel="stylesheet">
<style>
body{ padding-top: 70px; }
</style>
</head>
<body>
<div id='HEADER'></div>
<div class="container-fluid row justify-content-center">
<form class="col-md-7" action="upload.php" enctype="multipart/form-data" method="POST" id="uploadForm">
<div class="accordion mb-2" id="optionalInfo">
<div class="card">
<div class="card-header" id="opt_description">
<h5 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#upload" aria-expanded="true" aria-controls="upload">
Upload
</button>
</h5>
</div>
<div id="upload" class="collapse show" aria-labelledby="opt_pload" data-parent="#optionalInfo">
<div class="card-body row justify-content-center">
<input type="text" class="form-control col-md-8 mr-2" value="selected file..." disabled id="selectedFile">
<span class="align-middle">
<label for="uploadfile" class="custom-file-upload mt-2">
Select File
</label>
</span>
<input type="file" class="btn required" name="uploaded_file" id="uploadfile">
</div><!-- End of card-body -->
</div>
</div><!-- End of card -->
<div class="card">
<div class="card-header" id="opt_description">
<h5 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#description" aria-expanded="true" aria-controls="description">
Description
</button>
</h5>
</div>
<div id="description" class="collapse" aria-labelledby="opt_description" data-parent="#optionalInfo">
<div class="card-body">
<textarea class="form-control" aria-label="Description" name="description"></textarea>
</div><!-- End of card-body -->
</div>
</div><!-- End of card -->
<div class="card">
<div class="card-header" id="opt_tag">
<h5 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#tag" aria-expanded="true" aria-controls="tag">
Tag
</button>
</h5>
</div>
<div id="tag" class="collapse" aria-labelledby="opt_tag" data-parent="#optionalInfo">
<div class="card-body">
<div class="input-group mb-1">
<input type="text" class="form-control" id="tagInput">
<div class="input-group-append">
<span class="btn input-group-text btn-outline-secondary add-tag">+</span>
</div>
</div>
<h5 id="tags"></h5>
</div><!-- End of card-body -->
</div>
</div><!-- End of card -->
</div><!-- End of accordion -->
<input type="submit" class="btn btn-primary" value="Upload">
</form>
<script>
$('#uploadfile').bind('change', function() {
var fileName = '';
fileName = $(this).val().split('\\');
$('#selectedFile').attr("value", fileName[fileName.length-1]);
});
$(document)
.on('click', '.remove-me', function(){
var input_val = $(this).text();
$(this).remove();
$('input[value='+input_val+']').remove();
})
.keypress(function(e){
if(e.which == 13){
e.preventDefault();
if($('#tagInput').is(':focus')){
$('.add-tag').trigger('click');
}
else{
$('#uploadForm').trigger('submit');
}
}
});
$('.add-tag').click(function(){
var tag = $("#tagInput").val();
tag = tag.replace(/\W/g, '');
$('#tags').append("<span class='btn badge badge-info mr-1 remove-me'>"+tag+"</span>");
$('#tags').append("<input type='text' class='hidden' name='tags[]' value='"+tag+"'>");
$('#tagInput').val('');
var se = $('form').serialize();
});
$('form').submit(function(event){
if($('input.required').val().length === 0){
alert("Please select a file to upload!");
event.preventDefault();
}
});
</script>
</div><!-- End of container-fluid -->
</body>
</html>
<?php
session_start();
if(isset($_SESSION['IdUser']) && !empty($_FILES['uploaded_file']))
{
$path = "Music/".$_SESSION['UserName'];
if(!is_dir($path)){
if(!mkdir($path, 0777, true)){
echo "Failed to create new folder.";
}
}
$audio_name = basename($_FILES['uploaded_file']['name']);
$path = $path ."/" .$audio_name;
$avail_fmt = array("audio/mp4", "video/mp4", "audio/mpeg", "audio/ogg", "application/octet-stream");
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->file($_FILES['uploaded_file']['tmp_name']);
if(false === array_search($mime, $avail_fmt, true)){
ALERT("File format not supported: ".$mime);
return 0;
}
else if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
if(!updateDB($audio_name)){
ALERT("There was an error when updating the DB!");
return 0;
}
chmod($path, 0731);
ALERT($audio_name." has been uploaded");
}
else{
switch($_FILES['upfile']['error']){
case UPLOAD_ERR_OK:
ALERT("Upload ok, but fail at other place");
break;
case UPLOAD_ERR_NO_FILE:
ALERT("No file sent.");
break;
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
ALERT("Exceeded filesize limit.");
break;
default:
ALERT("Unknown errors.");
}
}
}
function updateDB($audio_name){
require_once "php/login2.php";
$conn = get_connection();
$stmt = $conn->stmt_init();
$query = "INSERT INTO Music(Name, UploadUser, MusicStoredPath, Description) VALUES('$audio_name', {$_SESSION['IdUser']}, ?, ?)";
if(!$stmt->prepare($query)){
ALERT("Failed to prepare query!");
$stmt->close();
$conn->close();
return 0;
}
$stmt->bind_param("ss", $path, $description);
$path = "{$_SESSION['UserName']}/$audio_name";
$description = isset($_POST['description']) ? $_POST['description'] : "";
if(!$stmt->execute()){
ALERT("Failed to update database!");
$stmt->close();
$conn->close();
return 0;
}
$mid = $stmt->insert_id;
$stmt->close();
// Insert tags
if(isset($_POST['tags'])){
$query = "INSERT INTO Tag(TagName, IdUser, IdMusic) VALUES(?, {$_SESSION['IdUser']}, $mid)";
$stmt = $conn->stmt_init();
$tmp = $stmt->prepare($query);
$stmt->bind_param('s', $tag);
$tags = $_POST['tags'];
foreach($tags as $tag){
$stmt->execute();
}
$result->free();
$stmt->close();
}
require_once "php/updateAccount.php";
update_updateDate($_SESSION['IdUser']);
$conn->close();
return 1;
}
function ALERT($msg){
echo "<script> alert('$msg'); </script>";
}
?>