File "uploadcontroller.php"
Full Path: /home/u593703731/domains/anandinternationalschool.com/public_html/controller/uploadcontroller.php
File size: 1.8 KB
MIME-type: text/x-php
Charset: utf-8
<?php
include "../admin/includes/conn.php";
if (isset($_POST['submit'])) {
$title =$_POST['title'];
$uploadDir = "assets/uploads/";
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0777, true);
}
$imageFiles = [];
$mediaFiles = [];
if (!empty($_FILES['images']['name'][0])) {
foreach ($_FILES['images']['name'] as $key => $name) {
if ($_FILES['images']['error'][$key] === 0) {
$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
$newName = "img_" . time() . "_" . rand(1000,9999) . "." . $ext;
move_uploaded_file(
$_FILES['images']['tmp_name'][$key],
$uploadDir . $newName
);
$imageFiles[] = $newName;
}
}
}
if (!empty($_FILES['medias']['name'][0])) {
foreach ($_FILES['medias']['name'] as $key => $name) {
if ($_FILES['medias']['error'][$key] === 0) {
$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
$newName = "media_" . time() . "_" . rand(1000,9999) . "." . $ext;
move_uploaded_file(
$_FILES['medias']['tmp_name'][$key],
$uploadDir . $newName
);
$mediaFiles[] = $newName;
}
}
}
$imagesJson = json_encode($imageFiles);
$mediasJson = json_encode($mediaFiles);
$sql = "INSERT INTO gallerys (images, medias,title)
VALUES ('$imagesJson', '$mediasJson','$title')";
if (mysqli_query($conn, $sql)) {
header("Location: ../admin/index.php?page=gallery");
exit;
} else {
echo "❌ Error: " . mysqli_error($conn);
}
}
?>