What are some common mistakes to avoid when creating links to PDF files using PHP?

One common mistake to avoid when creating links to PDF files using PHP is not properly handling file paths. It's important to ensure that the file paths are correct and that the files exist before creating links to them. Additionally, it's crucial to sanitize user input to prevent security vulnerabilities such as directory traversal attacks.

// Example of properly handling file paths and sanitizing user input
$filename = "example.pdf";
$filepath = "path/to/pdf/files/";

if (file_exists($filepath . $filename)) {
    $safeFilename = basename($filename); // Sanitize user input
    $link = "<a href='" . $filepath . $safeFilename . "'>Download PDF</a>";
    echo $link;
} else {
    echo "File not found.";
}