-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.php
More file actions
88 lines (72 loc) · 3.38 KB
/
Copy pathcreate.php
File metadata and controls
88 lines (72 loc) · 3.38 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="./download.jpg">
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
<title>Product | Create page</title>
</head>
<body class="bg-black text-white p-10">
<?php include 'navbar.php' ?>
<?php
$OpenFolder = "uploads/";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!is_dir($OpenFolder)) {
mkdir($OpenFolder, 0777, true);
}
$errors = [];
$file_name = $_FILES['image']['name'];
$file_size = $_FILES['image']['size'];
$file_type = $_FILES['image']['type'];
$file_temp = $_FILES['image']['tmp_name'];
$fileFormat = explode('/', $file_type);
$file_extension = strtolower(end($fileFormat));
$allowed_extensions = ["jpeg", "jpg", "png"];
if (!in_array($file_extension, $allowed_extensions)) {
$errors[] = "Only JPEG or PNG files are allowed.";
}
if ($file_size > 2097152) {
$errors[] = "File size must be 2MB or lower.";
}
if (empty($errors)) {
$newFileName = time() . "_" . basename($file_name);
move_uploaded_file($file_temp, $OpenFolder . $newFileName);
// Collect form data
$product = [
"name" => $_POST['name'],
"price" => $_POST['price'],
"image" => $newFileName
];
// Save into products.json
$dataFile = "products.json";
$products = [];
if (file_exists($dataFile)) {
$products = json_decode(file_get_contents($dataFile), true);
}
$products[] = $product;
file_put_contents($dataFile, json_encode($products, JSON_PRETTY_PRINT));
header("Location: index.php");
exit;
} else {
print_r($errors);
}
}
?>
<form action="./create.php" method="POST" enctype="multipart/form-data" class="max-w-md mx-auto bg-gray-900 rounded-xl shadow-lg p-8 flex flex-col gap-6">
<div>
<label class="block mb-2 text-sm font-medium text-gray-300">Upload an Image</label>
<input type="file" name="image" class="block w-full text-sm text-gray-200 bg-gray-800 rounded-lg border border-gray-700 focus:border-blue-500 focus:ring focus:ring-blue-500 focus:ring-opacity-50 p-2">
</div>
<div>
<label class="block mb-2 text-sm font-medium text-gray-300">Product Name</label>
<input type="text" name="name" class="block w-full p-2 rounded-lg bg-gray-800 text-white border border-gray-700 focus:border-blue-500 focus:ring focus:ring-blue-500 focus:ring-opacity-50" placeholder="Product Name">
</div>
<div>
<label class="block mb-2 text-sm font-medium text-gray-300">Price</label>
<input type="text" name="price" class="block w-full p-2 rounded-lg bg-gray-800 text-white border border-gray-700 focus:border-blue-500 focus:ring focus:ring-blue-500 focus:ring-opacity-50" placeholder="Price">
</div>
<input type="submit" class="mt-4 w-full py-2 px-4 bg-blue-600 cursor-pointer hover:bg-blue-700 text-white font-semibold rounded-lg shadow transition duration-200" value="Create Product">
</form>
</body>
</html>