Skip to content
PHP Logo

How can PHP developers determine the file extension of an uploaded file for validation purposes?

To determine the file extension of an uploaded file for validation purposes, PHP developers can use the `pathinfo()` function to extract the extension from the file name. This can help ensure that the uploaded file has the correct file type before processing it further.

php
// Get the file extension of the uploaded file
$filename = $_FILES['file']['name'];
$extension = pathinfo($filename, PATHINFO_EXTENSION);

// Validate the file extension
if ($extension == 'pdf') {
    // Process the uploaded PDF file
} else {
    echo "Invalid file type. Only PDF files are allowed.";
}