What are some common pitfalls when using PHP for file uploads, specifically in terms of verifying file types?
One common pitfall when using PHP for file uploads is not properly verifying the file types before allowing the upload. This can lead to security vulnerabilities such as allowing malicious files to be uploaded and executed on the server. To prevent this, it is important to validate the file type using a whitelist of allowed file extensions.
$allowed_extensions = array('jpg', 'jpeg', 'png', 'gif');
$uploaded_file = $_FILES['file']['name'];
$file_extension = pathinfo($uploaded_file, PATHINFO_EXTENSION);
if (!in_array($file_extension, $allowed_extensions)) {
die("Invalid file type. Only JPG, JPEG, PNG, and GIF files are allowed.");
}