-
-
Notifications
You must be signed in to change notification settings - Fork 266
/
upload.php
73 lines (61 loc) · 1.97 KB
/
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
69
70
71
72
73
<?php
require 'system/includes/dispatch.php';
require 'system/includes/session.php';
// Load the configuration file
config('source', 'config/config.ini');
// Set the timezone
if (config('timezone')) {
date_default_timezone_set(config('timezone'));
} else {
date_default_timezone_set('Asia/Jakarta');
}
$whitelist = array('jpg', 'jpeg', 'jfif', 'pjpeg', 'pjp', 'png', 'gif', 'webp');
$name = null;
$dir = 'content/images/';
$dirThumb = 'content/images/thumbnails/';
$error = null;
$timestamp = date('YmdHis');
$path = null;
$width = config('thumbnail.width');
if (login()) {
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
if (is_null($width) || empty($width)) {
$width = 500;
}
if (isset($_FILES) && isset($_FILES['file'])) {
$tmp_name = $_FILES['file']['tmp_name'];
$name = basename($_FILES['file']['name']);
$error = $_FILES['file']['error'];
$path = $dir . $timestamp . '-' . $name;
$check = getimagesize($tmp_name);
if($check !== false) {
if ($error === UPLOAD_ERR_OK) {
$extension = pathinfo($name, PATHINFO_EXTENSION);
if (!in_array(strtolower($extension), $whitelist)) {
$error = 'Invalid file type uploaded.';
} else {
move_uploaded_file($tmp_name, $path);
}
$imageFile = pathinfo($path, PATHINFO_FILENAME);
$thumbFile = $dirThumb . $imageFile. '-' . $width . '.webp';
if (!file_exists($thumbFile)) {
create_thumb($path, $width);
}
}
} else {
$error = "File is not an image.";
}
}
header('Content-Type: application/json');
echo json_encode(array(
'path' => $path,
'name' => $name,
'error' => $error,
));
die();
} else {
$login = site_url() . 'login';
header("location: $login");
}