File "delete-file.php"

Full Path: /home/analogde/www/07/endpoint/delete-file.php
File size: 1.88 KB
MIME-type: text/x-php
Charset: utf-8

<?php
include('../conn/conn.php');

if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET["file"])) {
    $fileID = $_GET["file"];

    // Get the file name from the database
    $sqlGetFileName = "SELECT file FROM tbl_file WHERE tbl_file_id = :fileID";
    $stmtGetFileName = $conn->prepare($sqlGetFileName);
    $stmtGetFileName->bindParam(':fileID', $fileID);
    $stmtGetFileName->execute();
    $fileName = $stmtGetFileName->fetchColumn();


    $uploadDirectory = "../file-uploads/";
    $filePath = $uploadDirectory . $fileName;
    
    if (file_exists($filePath) && unlink($filePath)) {
        // File in directory deleted successfully

        // Delete the file record from the database
        $sqlDeleteFile = "DELETE FROM tbl_file WHERE tbl_file_id = :fileID";
        $stmtDeleteFile = $conn->prepare($sqlDeleteFile);
        $stmtDeleteFile->bindParam(':fileID', $fileID);
        
        if ($stmtDeleteFile->execute()) {
            // File record deleted successfully
            echo "
            <script>
                alert('File deleted successfully!');
                window.location.href = 'http://localhost/file-manager-app/index.php';
            </script>
            ";
            exit;
        } else {
            echo "
            <script>
                alert('Error deleting file record from the database.');
                window.location.href = 'http://localhost/file-manager-app/index.php';
            </script>
            ";
        }
    } else {
        echo "
        <script>
            alert('Error deleting the file.');
            window.location.href = 'http://localhost/file-manager-app/index.php';
        </script>
        ";
    }
} else {
    echo "
    <script>
        alert('Invalid request.');
        window.location.href = 'http://localhost/file-manager-app/index.php';
    </script>
    ";
}
?>