-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload-1.php
75 lines (67 loc) · 1.96 KB
/
upload-1.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
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$title = htmlentities(strip_tags($_POST['title']));
$description = htmlentities(strip_tags($_POST['title']));
$tags = $_POST['tags'];
$positions = $_POST['positions'];
$image = $_FILES['image'];
if (!empty($title) && !empty($image)) {
$imagePath = uploadImage($image, 'image');
$arr = [];
if (count($tags) === count($positions)) {
foreach ($tags as $key => $index) {
$arr[] = [
"name" => $tag,
"position" => $positions[$key]
];
}
}
addNewData([
'title' => $title,
'description' => $description,
'path' => $imagePath,
'tags' => $arr,
]);
}
}
function addNewData($newData)
{
$data = file_get_contents('images.json');
$data = (array)json_decode($data);
$id = count($data) + 1;
$newData['id'] = $id;
$data[] = $newData;
file_put_contents('res/images.json', json_encode($data));
}
function uploadImage($image, $key)
{
$target_dir = "res/";
$target_file = $target_dir . basename($image[$key]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
$check = getimagesize($image[$key]["tmp_name"]);
if ($check !== false) {
$uploadOk = 1;
} else {
$uploadOk = 0;
}
if (file_exists($target_file)) {
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 5000000) {
$uploadOk = 0;
}
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif") {
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo false;
} else {
if (move_uploaded_file($image[$key]["tmp_name"], $target_file)) {
return $target_dir . basename($image[$key]["name"]);
} else {
return false;
}
}
}