Fixed some bugs
This commit is contained in:
@@ -5,6 +5,11 @@ require_once($rootPath . "/src/config/connection.php");
|
||||
require_once($rootPath . "/src/config/functions.php");
|
||||
session_start();
|
||||
|
||||
// Enable error reporting for debugging
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', 0); // Don't display, but log them
|
||||
ini_set('log_errors', 1);
|
||||
|
||||
if (!isset($_SESSION['user_id'])) {
|
||||
http_response_code(401);
|
||||
echo "Not authorized";
|
||||
@@ -32,36 +37,42 @@ echo $author_id;
|
||||
$cover_image_path = null;
|
||||
|
||||
// Only attempt upload if a file was submitted
|
||||
if (!empty($_FILES['cover_image']['name'])) {
|
||||
if (!empty($_FILES['cover_image']['name']) && $_FILES['cover_image']['error'] === UPLOAD_ERR_OK) {
|
||||
$uploadDir = $rootPath . "/uploads/blogs/" . $article_id . "/";
|
||||
if (!is_dir($uploadDir)) {
|
||||
mkdir($uploadDir, 0755, true);
|
||||
|
||||
// Create directory if it doesn't exist (match working pattern)
|
||||
if (!file_exists($uploadDir)) {
|
||||
mkdir($uploadDir, 0777, true);
|
||||
}
|
||||
|
||||
// Validate file using existing function
|
||||
$file_result = validateFileUpload($_FILES['cover_image'], 'profile_picture');
|
||||
if ($file_result === false) {
|
||||
// Simple validation - check extension
|
||||
$extension = strtolower(pathinfo($_FILES['cover_image']['name'], PATHINFO_EXTENSION));
|
||||
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
|
||||
|
||||
if (!in_array($extension, $allowedExtensions)) {
|
||||
http_response_code(400);
|
||||
echo "Invalid file upload";
|
||||
echo "Invalid file type. Allowed: jpg, jpeg, png, gif, webp";
|
||||
exit;
|
||||
}
|
||||
|
||||
// Use fixed filename "cover" to avoid creating multiple copies on autosave
|
||||
$extension = $file_result['extension'];
|
||||
$filename = "cover." . $extension;
|
||||
|
||||
// Delete old cover if it exists with different extension
|
||||
array_map('unlink', glob($uploadDir . "cover.*"));
|
||||
$oldCovers = glob($uploadDir . "cover.*");
|
||||
if ($oldCovers) {
|
||||
foreach ($oldCovers as $oldCover) {
|
||||
@unlink($oldCover);
|
||||
}
|
||||
}
|
||||
|
||||
$targetPath = $uploadDir . $filename;
|
||||
$cover_image_path = "/uploads/blogs/" . $article_id . "/" . $filename;
|
||||
|
||||
// Move the uploaded file
|
||||
if (move_uploaded_file($_FILES['cover_image']['tmp_name'], $targetPath)) {
|
||||
// File moved successfully, $cover_image_path is set
|
||||
} else {
|
||||
if (!move_uploaded_file($_FILES['cover_image']['tmp_name'], $targetPath)) {
|
||||
http_response_code(500);
|
||||
echo "Failed to move uploaded file.";
|
||||
echo "Failed to move uploaded file";
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user