What potential security risks should be considered when allowing file uploads in PHP?

When allowing file uploads in PHP, potential security risks include allowing malicious files to be uploaded, which can be executed on the server, leading to attacks like code injection or malware distribution. To mitigate these risks, it is important to validate file types, limit file sizes, store files in a secure directory outside of the web root, and use functions like `move_uploaded_file()` to handle file uploads securely.

// Validate file type
$allowedTypes = ['image/jpeg', 'image/png'];
if (!in_array($_FILES['file']['type'], $allowedTypes)) {
    die('Invalid file type.');
}

// Limit file size
$maxFileSize = 2 * 1024 * 1024; // 2MB
if ($_FILES['file']['size'] > $maxFileSize) {
    die('File size exceeds limit.');
}

// Store file in a secure directory
$uploadDir = '/var/www/uploads/';
$uploadFile = $uploadDir . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
    echo 'File uploaded successfully.';
} else {
    echo 'Failed to upload file.';
}