'Error saving the path of an image in my database

I want to save the path of an image in my database, these images are generated with html2canvas and are saved in a directory, but when I generate them and save them in my database, a non-existent image name is generated and that is the one that stores me.

This is the code of how the image is saved in the database:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    include './save.php'; 
       
    $file = './images';
    $route = $file. "/". $imgName;

    $insert = $conexion->query("INSERT INTO  images (img) VALUES ('$route')");
    
    if ($insert) {
        echo "<script>alert('successfully registered image')</script>";
    }
    else {
        echo '<script>("Ups...Something's wrong")</script>';
    }
}

This is the code that decodes the image for me and saves it in the folder:

$image = json_decode(file_get_contents("php://input"));


$image = $image->capture;

$image = str_replace("data:image/png;base64,", "", urldecode($image));
$image = base64_decode($image); 

$imgName = "Image_" . uniqid() . ".png";

file_put_contents("images/$imgName", $image);


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source