<?php
include "ChatConfig.php";
$files = scandir("upload/");
foreach ($files as $file) {
    $value = "https://app2.drsiamakheydari.ir/upload/" . $file;
    $query = $connection->prepare("SELECT * FROM group_1 WHERE path LIKE ?");
    $query->bindValue(1, "%$value%", PDO::PARAM_STR);
    $query->execute();
    if (!$query->rowCount() == 0) {
        while ($results = $query->fetch()) {
            echo $results["path"] . "<br />\n";
        }
    } else {
        $query1 = $connection->prepare(
            "SELECT * FROM group_2 WHERE path LIKE ?"
        );
        $query1->bindValue(1, "%$value%", PDO::PARAM_STR);
        $query1->execute();
        if (!$query1->rowCount() == 0) {
            while ($results1 = $query1->fetch()) {
                echo $results1["path"] . "<br />\n";
            }
        } else {
            $query2 = $connection->prepare(
                "SELECT * FROM group_3 WHERE path LIKE ?"
            );
            $query2->bindValue(1, "%$value%", PDO::PARAM_STR);
            $query2->execute();
            if (!$query2->rowCount() == 0) {
                while ($results2 = $query2->fetch()) {
                    echo $results2["path"] . "<br />\n";
                }
            } else {
                $query3 = $connection->prepare(
                    "SELECT * FROM group_4 WHERE path LIKE ?"
                );
                $query3->bindValue(1, "%$value%", PDO::PARAM_STR);
                $query3->execute();
                if (!$query3->rowCount() == 0) {
                    while ($results3 = $query3->fetch()) {
                        echo $results3["path"] . "<br />\n";
                    }
                } else {
                    $query4 = $connection->prepare(
                        "SELECT * FROM group_5 WHERE path LIKE ?"
                    );
                    $query4->bindValue(1, "%$value%", PDO::PARAM_STR);
                    $query4->execute();
                    if (!$query4->rowCount() == 0) {
                        while ($results4 = $query4->fetch()) {
                            echo $results4["path"] . "<br />\n";
                        }
                    } else {
                        unlink("upload/" . $file);
                    }
                }
            }
        }
    }
}
