What potential pitfalls should be considered when extracting file extensions from uploaded images in PHP?
When extracting file extensions from uploaded images in PHP, potential pitfalls to consider include: 1. Malicious users may attempt to manipulate the file extension to bypass security checks. 2. File extensions can be spoofed, leading to incorrect identification of file types. 3. Invalid or unexpected file extensions could result in errors or security vulnerabilities. To mitigate these risks, it is recommended to use PHP's built-in functions like `pathinfo()` to extract the file extension and validate it against a whitelist of allowed extensions.
$uploadedFile = $_FILES['image'];
$fileName = $uploadedFile['name'];
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
$allowedExtensions = array('jpg', 'jpeg', 'png', 'gif');
if (!in_array($extension, $allowedExtensions)) {
// Handle invalid file extension error
} else {
// Proceed with processing the uploaded image
}