What is the theoretical approach to integrating a image upload function into a PHP script for managing vehicle listings?

To integrate an image upload function into a PHP script for managing vehicle listings, you can use the following theoretical approach: 1. Create an HTML form with a file input field for uploading images. 2. Use PHP to handle the uploaded image file, validate it, and move it to a designated directory on the server. 3. Save the file path or name in the database along with other vehicle listing details.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $targetDir = "uploads/";
    $targetFile = $targetDir . basename($_FILES["image"]["name"]);
    $uploadOk = 1;
    
    // Check if image file is a actual image or fake image
    $check = getimagesize($_FILES["image"]["tmp_name"]);
    if($check !== false) {
        $uploadOk = 1;
    } else {
        $uploadOk = 0;
    }
    
    // Check if file already exists
    if (file_exists($targetFile)) {
        $uploadOk = 0;
    }
    
    // Upload file
    if ($uploadOk == 1) {
        if (move_uploaded_file($_FILES["image"]["tmp_name"], $targetFile)) {
            // File uploaded successfully, save file path to database
            $imagePath = $targetFile;
            
            // Save vehicle listing details to database
            // $make = $_POST["make"];
            // $model = $_POST["model"];
            // $year = $_POST["year"];
            // $price = $_POST["price"];
            // $description = $_POST["description"];
            
            // Save image path and other details to database
            // $sql = "INSERT INTO vehicle_listings (make, model, year, price, description, image_path) VALUES ('$make', '$model', '$year', '$price', '$description', '$imagePath')";
            // mysqli_query($conn, $sql);
            
            echo "The file ". htmlspecialchars( basename( $_FILES["image"]["name"])). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
}
?>