2025-06-06 17:14:52 +08:00

77 lines
2.2 KiB
PHP

<link rel="stylesheet" href="../../index.css" type="text/css" />
<script src="../../home.js"></script>
<html>
<head>
<title>Change img</title>
<style>
body{
align-items: center;
height: 100%;
width: 99%;
z-index: 1;
overflow: hidden;
background-image: none;
}
</style>
</head>
<body>
<h1>Change img</h1>
<?php
// Check if the form was submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Include the database connection file
include '../ConnectDB.php';
$name = $_GET['name'];
// Read the uploaded image file
$image = $_FILES["image"]["tmp_name"];
$imageData = file_get_contents($image);
// Check if the uploaded file is an image
$mime = mime_content_type($image);
$allowedMimeTypes = ['image/jpeg', 'image/png', 'image/gif'];
if (!in_array($mime, $allowedMimeTypes)) {
die("Invalid file format. Only JPEG, PNG, and GIF images are allowed.");
}
// Check if the file size is within limits
$maxFileSize = 1024 * 1024; // 1 MB
if ($_FILES["image"]["size"] > $maxFileSize) {
die("File size exceeds the limit. Maximum allowed size is 1 MB.");
}
// Create a connection to the database
$conn = new mysqli($servername, $username, $password, $db);
// Check for connection errors
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Prepare the SQL statement to update the image
$stmt = $conn->prepare("UPDATE inventory SET img = ? WHERE name = ?");
$stmt->bind_param("ss", $imageData, $name);
// Execute the SQL statement
if ($stmt->execute()) {
echo "Image uploaded successfully.";
} else {
echo "Error uploading image.";
}
// Close the statement
$stmt->close();
}
?>
<form method="POST" action="" enctype="multipart/form-data">
<label for="img">Img:</label>
<input type="file" name="image" accept="image/*" required>
<br><br>
<button type="submit">Change</button>
</form>
</body>
</html>