Fixed some bugs
This commit is contained in:
@@ -76,25 +76,29 @@ try {
|
||||
$updateStmt->close();
|
||||
|
||||
// Handle cover image upload if provided
|
||||
if (isset($_FILES['cover_image']) && $_FILES['cover_image']['error'] !== UPLOAD_ERR_NO_FILE) {
|
||||
$allowedMimes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
|
||||
$maxSize = 5 * 1024 * 1024; // 5MB
|
||||
|
||||
if (isset($_FILES['cover_image']) && $_FILES['cover_image']['error'] === UPLOAD_ERR_OK) {
|
||||
$fileName = $_FILES['cover_image']['name'];
|
||||
$fileTmpName = $_FILES['cover_image']['tmp_name'];
|
||||
$fileSize = $_FILES['cover_image']['size'];
|
||||
$fileMime = mime_content_type($fileTmpName);
|
||||
|
||||
// Validate file
|
||||
if (!in_array($fileMime, $allowedMimes)) {
|
||||
throw new Exception('Invalid cover image file type');
|
||||
|
||||
// Validate file extension
|
||||
$ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
|
||||
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
|
||||
|
||||
if (!in_array($ext, $allowedExtensions)) {
|
||||
throw new Exception('Invalid cover image file type. Allowed: jpg, jpeg, png, gif, webp');
|
||||
}
|
||||
|
||||
if ($fileSize > $maxSize) {
|
||||
if ($fileSize > 5 * 1024 * 1024) {
|
||||
throw new Exception('Cover image file too large (max 5MB)');
|
||||
}
|
||||
|
||||
$albumDir = $rootPath . '/assets/uploads/gallery/' . $album_id;
|
||||
|
||||
// Create directory if it doesn't exist (match working pattern)
|
||||
if (!file_exists($albumDir)) {
|
||||
mkdir($albumDir, 0777, true);
|
||||
}
|
||||
|
||||
// Delete old cover if it exists
|
||||
$oldCoverStmt = $conn->prepare("SELECT cover_image FROM photo_albums WHERE album_id = ?");
|
||||
@@ -104,16 +108,15 @@ try {
|
||||
if ($oldCoverResult->num_rows > 0) {
|
||||
$oldCover = $oldCoverResult->fetch_assoc();
|
||||
if ($oldCover['cover_image']) {
|
||||
$oldCoverPath = $_SERVER['DOCUMENT_ROOT'] . $oldCover['cover_image'];
|
||||
$oldCoverPath = $rootPath . $oldCover['cover_image'];
|
||||
if (file_exists($oldCoverPath)) {
|
||||
unlink($oldCoverPath);
|
||||
@unlink($oldCoverPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
$oldCoverStmt->close();
|
||||
|
||||
// Generate unique filename
|
||||
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
|
||||
$newFileName = 'cover_' . uniqid() . '.' . $ext;
|
||||
$filePath = $albumDir . '/' . $newFileName;
|
||||
$coverImagePath = '/assets/uploads/gallery/' . $album_id . '/' . $newFileName;
|
||||
@@ -130,12 +133,15 @@ try {
|
||||
}
|
||||
|
||||
// Handle photo uploads if any
|
||||
if (isset($_FILES['photos']) && $_FILES['photos']['error'][0] !== UPLOAD_ERR_NO_FILE) {
|
||||
$allowedMimes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
|
||||
if (isset($_FILES['photos']) && $_FILES['photos']['error'][0] === UPLOAD_ERR_OK) {
|
||||
$maxSize = 5 * 1024 * 1024; // 5MB
|
||||
|
||||
$albumDir = $rootPath . '/assets/uploads/gallery/' . $album_id;
|
||||
|
||||
// Create directory if it doesn't exist (match working pattern)
|
||||
if (!file_exists($albumDir)) {
|
||||
mkdir($albumDir, 0777, true);
|
||||
}
|
||||
|
||||
// Get current max display order
|
||||
$orderStmt = $conn->prepare("SELECT MAX(display_order) as max_order FROM photos WHERE album_id = ?");
|
||||
$orderStmt->bind_param("i", $album_id);
|
||||
@@ -153,15 +159,17 @@ try {
|
||||
$fileName = $_FILES['photos']['name'][$i];
|
||||
$fileTmpName = $_FILES['photos']['tmp_name'][$i];
|
||||
$fileSize = $_FILES['photos']['size'][$i];
|
||||
$fileMime = mime_content_type($fileTmpName);
|
||||
|
||||
// Validate file
|
||||
if (!in_array($fileMime, $allowedMimes)) {
|
||||
throw new Exception('Invalid file type: ' . $fileName);
|
||||
|
||||
// Validate file extension
|
||||
$ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
|
||||
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
|
||||
|
||||
if (!in_array($ext, $allowedExtensions)) {
|
||||
throw new Exception('Invalid file type: ' . $fileName . '. Allowed: jpg, jpeg, png, gif, webp');
|
||||
}
|
||||
|
||||
if ($fileSize > $maxSize) {
|
||||
throw new Exception('File too large: ' . $fileName);
|
||||
throw new Exception('File too large: ' . $fileName . ' (max 5MB)');
|
||||
}
|
||||
|
||||
// Generate unique filename
|
||||
|
||||
Reference in New Issue
Block a user