What is the purpose of restricting file uploads to only PDF files in PHP?

Restricting file uploads to only PDF files in PHP helps prevent security vulnerabilities such as malicious code execution through file uploads. By limiting uploads to PDF files, you can ensure that only safe and trusted files are accepted by your application.

// Check if the uploaded file is a PDF
$allowedExtensions = ['pdf'];
$uploadedFileExtension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

if (!in_array($uploadedFileExtension, $allowedExtensions)) {
    // File extension is not allowed, handle error
    echo "Only PDF files are allowed for upload.";
} else {
    // Process the uploaded PDF file
    // Move the file to the desired location
    move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
    echo "File uploaded successfully.";
}