<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");
$uploadDir = __DIR__ . "/images/";
// Ensure directory exists
if (!file_exists($uploadDir)) {
mkdir($uploadDir, 0755, true);
}
// No file field at all
if (!isset($_FILES['image'])) {
echo json_encode([
'success' => false,
'error' => 'No file uploaded (field "image" missing).'
]);
exit;
}
$file = $_FILES['image'];
// Handle PHP upload errors explicitly
if ($file['error'] !== UPLOAD_ERR_OK) {
$errorMsg = 'Unknown error';
switch ($file['error']) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$errorMsg = 'File is too large. Check upload_max_filesize/post_max_size in PHP.';
break;
case UPLOAD_ERR_PARTIAL:
$errorMsg = 'File was only partially uploaded.';
break;
case UPLOAD_ERR_NO_FILE:
$errorMsg = 'No file was uploaded.';
break;
case UPLOAD_ERR_NO_TMP_DIR:
$errorMsg = 'Missing temporary folder on server.';
break;
case UPLOAD_ERR_CANT_WRITE:
$errorMsg = 'Failed to write file to disk.';
break;
case UPLOAD_ERR_EXTENSION:
$errorMsg = 'File upload stopped by a PHP extension.';
break;
}
echo json_encode([
'success' => false,
'error' => 'Upload error: ' . $errorMsg,
'code' => $file['error']
]);
exit;
}
// Extra sanity check
if (empty($file['tmp_name']) || !is_uploaded_file($file['tmp_name'])) {
echo json_encode([
'success' => false,
'error' => 'Temporary upload file missing or invalid.'
]);
exit;
}
// Try to detect real MIME type from file contents (if extension is available)
$detectedType = null;
if (function_exists('mime_content_type')) {
$detectedType = @mime_content_type($file['tmp_name']);
}
$allowedTypes = [
'image/jpeg',
'image/png',
'image/gif',
'image/webp'
];
// If mime_content_type worked and is allowed, use it. Otherwise fall back to extension.
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
// Map common extensions to MIME if detection failed
$extToMime = [
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'webp' => 'image/webp'
];
if (!$detectedType && isset($extToMime[$ext])) {
$detectedType = $extToMime[$ext];
}
if (!$detectedType || !in_array($detectedType, $allowedTypes, true)) {
echo json_encode([
'success' => false,
'error' => 'Invalid file type. Only JPG, PNG, GIF, and WEBP allowed.',
'detected_type' => $detectedType,
'original_type' => $file['type'],
'extension' => $ext
]);
exit;
}
// Normalize extension from detected MIME (ensures proper extension)
$mimeToExt = [
'image/jpeg' => 'jpg',
'image/png' => 'png',
'image/gif' => 'gif',
'image/webp' => 'webp'
];
$finalExt = $mimeToExt[$detectedType] ?? $ext ?: 'jpg';
// Generate unique filename
$filename = uniqid('img_') . '.' . $finalExt;
$targetPath = $uploadDir . $filename;
if (move_uploaded_file($file['tmp_name'], $targetPath)) {
echo json_encode([
'success' => true,
'url' => 'images/' . $filename,
'debug' => [
'detected_type' => $detectedType,
'original_type' => $file['type'],
'extension' => $ext
]
]);
} else {
echo json_encode([
'success' => false,
'error' => 'Failed to save file (move_uploaded_file returned false).',
'debug' => [
'targetPath' => $targetPath,
'is_writable_uploadDir' => is_writable($uploadDir)
]
]);
}
?>