What are the potential security risks of allowing users to directly input file paths in PHP file uploads?

Allowing users to directly input file paths in PHP file uploads can pose security risks such as directory traversal attacks, where users can potentially access and upload files from unintended directories on the server. To mitigate this risk, it is important to sanitize user input and restrict file uploads to a specific directory on the server.

// Sanitize user input and restrict file uploads to a specific directory
$uploadDir = 'uploads/';
$uploadFile = $uploadDir . basename($_FILES['userfile']['name']);

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadFile)) {
    echo "File is valid, and was successfully uploaded.";
} else {
    echo "Possible file upload attack!";
}