54 lines
1.8 KiB
PHP
54 lines
1.8 KiB
PHP
<?php
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['logoFile'])) {
|
|
$targetDir = "assets/images/";
|
|
// Ensure the target directory exists
|
|
if (!is_dir($targetDir)) {
|
|
mkdir($targetDir, 0775, true);
|
|
}
|
|
|
|
$original_filename = $_FILES["logoFile"]["name"];
|
|
$imageFileType = strtolower(pathinfo($original_filename, PATHINFO_EXTENSION));
|
|
|
|
// Use a consistent name for the logo file to make it easy to reference
|
|
$targetFile = $targetDir . "logo." . $imageFileType;
|
|
|
|
// Check if image file is a actual image or fake image
|
|
$check = getimagesize($_FILES["logoFile"]["tmp_name"]);
|
|
if($check === false) {
|
|
header("Location: admin.php?error=File is not an image.");
|
|
exit;
|
|
}
|
|
|
|
// Allow certain file formats
|
|
$allowed_types = ["jpg", "png", "jpeg", "gif"];
|
|
if(!in_array($imageFileType, $allowed_types)) {
|
|
header("Location: admin.php?error=Sorry, only JPG, JPEG, PNG & GIF files are allowed.");
|
|
exit;
|
|
}
|
|
|
|
// Before uploading, remove any old logo files to avoid conflicts
|
|
$existing_logos = glob($targetDir . "logo.*_old");
|
|
foreach ($existing_logos as $old_logo) {
|
|
unlink($old_logo);
|
|
}
|
|
|
|
// Check if a logo already exists and rename it
|
|
$current_logo_path = glob($targetDir . "logo.*_old");
|
|
if (!empty($current_logo_path)) {
|
|
rename($current_logo_path[0], $targetDir . "logo." . pathinfo($current_logo_path[0], PATHINFO_EXTENSION) . "_old");
|
|
}
|
|
|
|
// Try to upload file
|
|
if (move_uploaded_file($_FILES["logoFile"]["tmp_name"], $targetFile)) {
|
|
header("Location: admin.php?success=1");
|
|
exit;
|
|
} else {
|
|
header("Location: admin.php?error=Sorry, there was an error uploading your file.");
|
|
exit;
|
|
}
|
|
} else {
|
|
// Redirect back to admin page if accessed directly
|
|
header("Location: admin.php");
|
|
exit;
|
|
}
|
|
?>
|