What are some best practices for storing PDF files in a local "mini-database" using PHP?

Storing PDF files in a local "mini-database" using PHP requires a structured approach to ensure efficient storage and retrieval. One best practice is to store the file path in the database rather than the actual file itself, as storing files directly in the database can lead to performance issues. Additionally, using unique identifiers for each file can help in organizing and retrieving the files easily.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Store PDF file in a directory on the server
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["pdf_file"]["name"]);

if (move_uploaded_file($_FILES["pdf_file"]["tmp_name"], $target_file)) {
    // Store file path in the database
    $sql = "INSERT INTO pdf_files (file_path) VALUES ('$target_file')";
    
    if ($conn->query($sql) === TRUE) {
        echo "File uploaded and stored in the database successfully.";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
} else {
    echo "Error uploading file.";
}

// Close database connection
$conn->close();