<?php 


include("database.php");

 ?>


<?php


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

if ($_FILES['pdfFile']['name']) {
    $uploadDir = "upload/"; 
    $fileName = uniqid() . "_" . basename($_FILES["pdfFile"]["name"]);
    $targetFile = $uploadDir . $fileName;

    if (move_uploaded_file($_FILES["pdfFile"]["tmp_name"], $targetFile)) {
        $websiteName = $_POST['website_name'];
        $uniqueUrl = uniqid(); 

        $sql = "INSERT INTO pdf_files (website_name, pdf_file, unique_url) VALUES ('$websiteName', '$targetFile', '$uniqueUrl')";
        
        if ($conn->query($sql) === TRUE) {
            echo json_encode(["status" => "success", "pdfUrl" => $targetFile, "uniqueUrl" => $uniqueUrl]);
        } else {
            echo json_encode(["status" => "error", "message" => "Database error"]);
        }
    } else {
        echo json_encode(["status" => "error", "message" => "Upload failed"]);
    }
}

$conn->close();
?>
