What are common issues when uploading avatars in PHP forums?
Common issues when uploading avatars in PHP forums include file size restrictions, file type restrictions, and security vulnerabilities. To solve these issues, you can validate the file size and type before uploading it, and use proper security measures such as sanitizing input and storing files in a secure directory.
// Check file size
$maxFileSize = 2 * 1024 * 1024; // 2MB
if ($_FILES['avatar']['size'] > $maxFileSize) {
echo "File size exceeds limit.";
exit;
}
// Check file type
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
if (!in_array($_FILES['avatar']['type'], $allowedTypes)) {
echo "Invalid file type.";
exit;
}
// Sanitize input
$avatarName = basename($_FILES['avatar']['name']);
$avatarPath = 'uploads/' . $avatarName;
// Move uploaded file to secure directory
move_uploaded_file($_FILES['avatar']['tmp_name'], $avatarPath);