What potential security risks should be considered when using PHP to search for and save file paths locally?

When using PHP to search for and save file paths locally, one potential security risk to consider is the possibility of directory traversal attacks. This occurs when an attacker manipulates file paths to access files outside of the intended directory. To prevent this, it is important to sanitize and validate user input before using it to construct file paths.

// Sanitize and validate user input for file paths
$user_input = $_POST['file_path'];
$cleaned_file_path = realpath('./uploads/' . basename($user_input));

// Save the file using the sanitized file path
file_put_contents($cleaned_file_path, $file_contents);