What potential pitfalls should be aware of when uploading PDF files using PHP?

When uploading PDF files using PHP, one potential pitfall to be aware of is the risk of allowing malicious files to be uploaded to your server. To prevent this, you should validate the file type to ensure it is a PDF before saving it to your server. Additionally, consider restricting the file size and implementing proper file handling techniques to prevent any security vulnerabilities.

// Validate file type before uploading
$allowedTypes = ['application/pdf'];
$uploadedFileType = $_FILES['file']['type'];

if (!in_array($uploadedFileType, $allowedTypes)) {
    die('Invalid file type. Please upload a PDF file.');
}

// Validate file size before uploading
$maxFileSize = 5242880; // 5MB in bytes
$uploadedFileSize = $_FILES['file']['size'];

if ($uploadedFileSize > $maxFileSize) {
    die('File size is too large. Please upload a file smaller than 5MB.');
}

// Save the file to the server
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);