-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathaudio.php
More file actions
39 lines (39 loc) · 1.19 KB
/
Copy pathaudio.php
File metadata and controls
39 lines (39 loc) · 1.19 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
<?php
header('Access-Control-Allow-Origin: *');
function generateRandomString($length = 5) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
if(isset($_FILES["audio"])){
if ($_FILES['audio']['error'] !== UPLOAD_ERR_OK) {
http_response_code(400);
exit;
}
$finfo = new finfo(FILEINFO_MIME_TYPE);
if ($finfo === false) {
$mime = $_FILES['audio']['type'];
} else {
$mime = $finfo->file($_FILES['audio']['tmp_name']);
}
$allowedMimes = ['audio/mpeg', 'audio/mp3'];
if (!in_array($mime, $allowedMimes)) {
http_response_code(400);
echo "Invalid file type";
exit;
}
$fileName = generateRandomString();
$targetPath = "audios/" . $fileName . '.mp3';
if (!is_dir("audios")) mkdir("audios");
if (move_uploaded_file($_FILES['audio']['tmp_name'], $targetPath)) {
echo $fileName;
} else {
http_response_code(500);
echo "Error saving file";
}
}
?>