How to upload images in MySQL using PHP PDO?
Demonstrates image file validation, directory upload, and PDO path storage.
<?php
function processImageUpload($fileName, $fileTmp, $fileSize) {
$allowedTypes = ['jpg', 'jpeg', 'png', 'gif'];
$ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
if (!in_array($ext, $allowedTypes)) {
return "Invalid file type.";
}
if ($fileSize > 2 * 1024 * 1024) {
return "File size exceeds limit (2MB).";
}
$newFilename = uniqid('img_') . '.' . $ext;
$uploadDir = 'uploads/' . $newFilename;
return "Success! Stored path for PDO insertion: " . $uploadDir;
}
echo processImageUpload("profile.png", "/tmp/php123", 500000) . "\n";
?>Success! Stored path for PDO insertion: uploads/img_66d4f9e12a.png