Vaihda mime_content_type → getimagesize, lisää testiendpoint

mime_content_type() vaatii fileinfo-laajennuksen joka ei ehkä ole
käytössä Pleskissä → PHP kaatuu fatal erroriin → HTTP 500 tyhjä body.
getimagesize() on osa GD-kirjastoa, lähes aina saatavilla.

Testiendpoint: upload.php?test kertoo onko PHP käynnissä ja mitkä
laajennukset on saatavilla.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-08 01:22:44 +02:00
parent bb9541d526
commit bd01662032

View File

@@ -6,8 +6,15 @@
ob_start(); ob_start();
error_reporting(0); error_reporting(0);
$allowed_mime = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']; // Testiendpoint: avaa upload.php?test selaimessa — pitäisi näyttää {"ok":"php_running"}
$max_bytes = 20 * 1024 * 1024; // 20 Mt if (isset($_GET['test'])) {
ob_clean();
header('Content-Type: application/json');
echo json_encode(['ok' => 'php_running', 'gd' => function_exists('getimagesize'), 'fileinfo' => function_exists('mime_content_type')]);
exit;
}
$max_bytes = 20 * 1024 * 1024; // 20 Mt
header('Content-Type: application/json'); header('Content-Type: application/json');
@@ -42,11 +49,15 @@ if ($f['size'] > $max_bytes) {
exit; exit;
} }
$mime = mime_content_type($f['tmp_name']); // getimagesize() on osa GD-kirjastoa (ei vaadi fileinfo-laajennusta)
if (!in_array($mime, $allowed_mime, true)) { $imginfo = @getimagesize($f['tmp_name']);
$type_map = [IMAGETYPE_JPEG => 'image/jpeg', IMAGETYPE_PNG => 'image/png',
IMAGETYPE_GIF => 'image/gif', IMAGETYPE_WEBP => 'image/webp'];
$mime = $imginfo ? ($type_map[$imginfo[2]] ?? null) : null;
if (!$mime) {
ob_clean(); ob_clean();
http_response_code(415); http_response_code(415);
echo json_encode(['error' => 'Only images (jpeg/png/gif/webp) allowed']); echo json_encode(['error' => 'Vain kuvat (jpeg/png/gif/webp) hyväksytään']);
exit; exit;
} }