What are potential pitfalls to avoid when uploading files in PHP applications?

One potential pitfall when uploading files in PHP applications is not validating the file type before allowing it to be uploaded. This can lead to security vulnerabilities such as allowing users to upload malicious files. To avoid this, always validate the file type before processing the upload.

// Validate file type before uploading
$allowedFileTypes = ['jpg', 'jpeg', 'png', 'gif'];
$uploadedFileType = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

if (!in_array($uploadedFileType, $allowedFileTypes)) {
    echo 'Invalid file type. Only JPG, JPEG, PNG, and GIF files are allowed.';
} else {
    // Process file upload
}