-
Notifications
You must be signed in to change notification settings - Fork 0
/
proses-upload.php
68 lines (56 loc) · 2.78 KB
/
proses-upload.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
<?php
session_start();
// Token CSRF
$csrfToken = $_SESSION['csrf_token'];
$uploadDir = 'file/'; // Ganti dengan direktori penyimpanan yang sesuai
$maxFileSize = 5 * 1024 * 1024; // Batasan ukuran file (dalam bytes), di sini 5 MB
// Validasi ukuran file
if (!isset($_POST['csrf_token']) || $_POST['csrf_token'] !== $csrfToken) {
$_SESSION['msg'] = '<div class="alert alert-info"><div class="alert-message">Token Invalid</div></div>';
header('location: ./');
}
else
{
if ($_FILES['file']['size'] > $maxFileSize) {
$_SESSION['msg'] = '<div class="alert alert-info"><div class="alert-message">Ukuran File Max 5 MB</div></div>';
header('location: ./');
}
else
{
// Validasi ekstensi file
$allowedExtensions = ['jpg', 'jpeg', 'png'];
$fileExtension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if (!in_array(strtolower($fileExtension), $allowedExtensions)) {
$_SESSION['msg'] = '<div class="alert alert-danger"><div class="alert-message">Extensi File Tidak Valid. Format harus jpg,jpeg,png</div></div>';
header('location: ./');
}
else
{
// Validasi tipe MIME
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES['file']['tmp_name']);
finfo_close($finfo);
$allowedMimeTypes = ['image/jpeg', 'image/png', 'image/jpg'];
if (!in_array($mime, $allowedMimeTypes)) {
$_SESSION['msg'] = '<div class="alert alert-warning"><div class="alert-message">File Tidak Valid ! Jangan Memodifikasi File Sembarangan !</div></div>';
header('location: ./');
}
else
{
// Membuat nama file yang aman
$fileName = uniqid() . '.' . $fileExtension;
$filePath = $uploadDir . $fileName;
// Simpan file ke direktori penyimpanan
if (move_uploaded_file($_FILES['file']['tmp_name'], $filePath)) {
$_SESSION['msg'] = '<div class="alert alert-success"><div class="alert-message">Berhasil Upload File</div></div>';
unset($_SESSION['csrf_token']);
header('location: ./');
} else {
$_SESSION['msg'] = '<div class="alert alert-danger"><div class="alert-message">Gagal Upload File</div></div>';
header('location: ./');
}
}
}
}
}
?>