What are some potential security risks associated with allowing users to upload PDF files through a PHP script?

Allowing users to upload PDF files through a PHP script can pose security risks such as file upload vulnerabilities, potential execution of malicious code, and server overload with large files. To mitigate these risks, it is important to validate the uploaded file type, restrict file size, and store the files in a secure location on the server.

// Validate uploaded file type
$allowed_extensions = array('pdf');
$uploaded_file_extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if(!in_array($uploaded_file_extension, $allowed_extensions)){
    die('Invalid file type. Only PDF files are allowed.');
}

// Restrict file size
$max_file_size = 5242880; // 5MB
if($_FILES['file']['size'] > $max_file_size){
    die('File size exceeds the limit of 5MB.');
}

// Store the file in a secure location on the server
$upload_directory = 'uploads/';
$uploaded_file_path = $upload_directory . basename($_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $uploaded_file_path)){
    echo 'File uploaded successfully.';
} else {
    echo 'File upload failed.';
}