php – Overwrite files once the database reaches a specified number of posts

Question

Hello I am making a system in php in which there can only be a limited number of images in the database and when someone uploads new photos, the old ones will be deleted from the database. I currently have this:

<?php 

if (isset($_POST['submit']) && isset($_FILES['my_image'])) {
    include "db_conn.php";

    echo "<pre>";
    print_r($_FILES['my_image']);
    echo "</pre>";

    $img_name = $_FILES['my_image']['name'];
    $img_size = $_FILES['my_image']['size'];
    $tmp_name = $_FILES['my_image']['tmp_name'];
    $error = $_FILES['my_image']['error'];

    if ($error === 0) {
        if ($img_size > 125000) {
            $em = "Sorry, your file is too large.";
            header("Location: index.php?error=$em");
        }else {
            $img_ex = pathinfo($img_name, PATHINFO_EXTENSION);
            $img_ex_lc = strtolower($img_ex);

            $allowed_exs = array("jpg", "jpeg", "png"); 

            if (in_array($img_ex_lc, $allowed_exs)) {
                $new_img_name = uniqid("IMG-", true).'.'.$img_ex_lc;
                $img_upload_path="uploads/".$new_img_name;
                move_uploaded_file($tmp_name, $img_upload_path);

                // Insert into Database
                $sql = "INSERT INTO images(image_url) 
                        VALUES('$new_img_name')";
                mysqli_query($conn, $sql);
                header("Location: index.php");
            }else {
                $em = "You can't upload files of this type";
                header("Location: index.php?error=$em");
            }
        }
    }else {
        $em = "unknown error occurred!";
        header("Location: index.php?error=$em");
    }

}else {
    header("Location: index.php");
}

Could you help me find the code or reference site? Thank you.

0
srgo 2 months 2021-06-02T03:50:39-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse