vendredi 23 avril 2021

Problem with uploading new image and hold the old one

i want to create a condition that checks if there is new image and will combine the old with the new one. When I don't upload an image and save the form, an empty image is added. What is the best option for doing this? Note: the point is that I want to combine the new image with the old image via the upload button and also want to implode. Thanks for your help.

    // Files array
    $files = [];
    if($_FILES['product_image']['size'] != 0 && $_FILES['product_image']['error'] != 0)
    {
        foreach($_FILES['product_image']['name'] as $i => $name) 
        {
            $name = $_FILES['product_image']['name'][$i];
            $tmp = $_FILES['product_image']['tmp_name'][$i];
    
            $path = "../images/$product_location/";
            $path = $path . basename($name);
    
            $files['file_name'][] = substr($path, 3);
            if(move_uploaded_file($tmp, $path)) 
            {
                echo '<p>The file <b>'.$name.'</b> successful upload</p>';
            }
        }
        // if hidden pic is not found then do not combine
        if(!isset($_POST['hidden_pic']))
        {
            $images = implode(",", $files['file_name']);
        }
        // combine the values
        else
        {
            $values = array_merge($_POST['hidden_pic'], $files['file_name']);
            $images = implode(",", $values);
        }
        // Product description
        $product_desc = $_POST['product_desc'];
    
        // Product brand
        $product_brand = $_POST['product_brand'];
    
        // Product model
        $product_model = $_POST['product_model'];
    
        // Product price
        $product_price = $_POST['product_price'];
    
        // Edit which date and time
        $date = date("Y-m-d H:i:s");
    
        // edit product info
        $editProduct = $user->editProduct($images, $product_brand, $product_model, $product_desc, $product_price, $product_occasion, $date);
     
        if ($editProduct[0] === true)
        {
            $_SESSION['PRODUCT_SUCCESS'] = $editProduct[1];
            // header('Location: ../product.php');
            echo '1';
            var_dump($product_occasion);
            die;
        }
        else
        {
            $_SESSION['PRODUCT_ERROR'] = $editProduct[1];
            header('Location: ../modify-product.php');
        }
    }
    else
    {
        $images = implode(",", $_POST['hidden_pic']);
        // Product description
        $product_desc = $_POST['product_desc'];
        // Product brand
        $product_brand = $_POST['product_brand'];
        // Product model
        $product_model = $_POST['product_model'];
        // Product price
        $product_price = $_POST['product_price'];
        // Edit which date and time
        $date = date("Y-m-d H:i:s");
        // edit product info
        $editProduct = $user->editProduct($images, $product_brand, $product_model, $product_desc, $product_price, $product_occasion, $date);
     
        if ($editProduct[0] === true)
        {
            $_SESSION['PRODUCT_SUCCESS'] = $editProduct[1];
            // header('Location: ../product.php');
            echo '2';
            die;
        }
        else
        {
            $_SESSION['PRODUCT_ERROR'] = $editProduct[1];
            header('Location: ../modify-product.php');
        }
    }

Aucun commentaire:

Enregistrer un commentaire