Manada
Manada

Reputation: 81

Php + Mysql + Multiupload Pics Files

I use a form to upload pics. I have 2 tables, one is product_profile and the other product_image.

In my form i have 2 buttons. One to upload the pic for product_profile and the other to Multiupload the pics in product_image.

But now i have a problem with if, else if and else. It's between "//Check all fields are not empty"

When i insert files only for product_image "else if(empty($imgFileProduct))" without insert for profile_image "else if(empty($imgFile))" that's works correctly with the message "Please Select Image File."

But when i insert only for profile_image, he insert into database and upload the file without check if product_image is empty or not and i have the follow message "Sorry, only JPG, JPEG, PNG & GIF files are allowed2." and not "Please Select Image File(S)."

Thanks for the help, all the day in this.

    <?php
    error_reporting( ~E_NOTICE );
    require_once 'dbconfig.php';
    if(isset($_POST['btnsave']))
    {
        $catalogname = $_POST['catalog_name'];
        $catalogmaker = $_POST['catalog_maker'];
        $catalogtypes = $_POST['catalog_types'];
        $catalogscale = $_POST['catalog_scale'];
        $catalogedition = $_POST['catalog_edition'];
        $imgFile = $_FILES['profile_image']['name'];
        $tmp_dir = $_FILES['profile_image']['tmp_name'];
        $imgSize = $_FILES['profile_image']['size'];
        $imgFileProduct = $_FILES['product_image']['name'];
        $imgSizeProduct = $_FILES['product_image']['tmp_name'];
        $tmp_dirProduct = $_FILES['product_image']['size'];
        //Check all fields are not empty
        if(empty($catalogname)){
            $errMSG = "Please Enter Product Name.";
        }
                else if(empty($catalogmaker)){
            $errMSG = "Please Enter a Maker.";
        }
                else if(empty($catalogtypes)){
            $errMSG = "Please Enter a Types.";
        }
                else if(empty($catalogscale)){
            $errMSG = "Please Enter a Scale.";
        }
                else if(empty($catalogedition)){
            $errMSG = "Please Enter a Edition.";
        }
                else if(empty($imgFile)){
            $errMSG = "Please Select Image File.";
        }
                else if(empty($imgFileProduct)){
            $errMSG = "Please Select Image File(S).";
        }
        //Check all fields are not empty
        else
        {
            $upload_dir = 'product_images/'; // upload directory
            $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
            $valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
            $userpic= rand(1000,1000000).".".$imgExt;
            if(in_array($imgExt, $valid_extensions)){
                if($imgSize < 5000000){
                    move_uploaded_file($tmp_dir,$upload_dir.$userpic);
                }
                else{
                    $errMSG = "Sorry, your file is too large.";
                }
            }
            else{
                $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed3.";   
            }
            if(!isset($errMSG)){
            $stmt = $DB_con->prepare('INSERT INTO id_catalog(name,maker,types,scale,edition,pic) VALUES(:uname, :umaker, :utypes, :uscale, :uedition, :upic)');
            $stmt->bindParam(':uname',$catalogname);
            $stmt->bindParam(':umaker',$catalogmaker);
            $stmt->bindParam(':utypes',$catalogtypes);
            $stmt->bindParam(':uscale',$catalogscale);
            $stmt->bindParam(':uedition',$catalogedition);
            $stmt->bindParam(':upic',$userpic);
            if($stmt->execute()){
                $successMSG = "new record succesfully inserted ...";
                header("refresh:5;index.php"); // redirects image view page after 5 seconds.
                $last_id = $DB_con->lastInsertId();
                echo "New record created successfully. Last inserted ID is: " . $last_id;
            }
            else{
                $errMSG = "error while inserting....";
            }
        }
            {
            foreach($_FILES['product_image']['tmp_name'] as $key => $tmp_dirProduct ){
            $imgFileProduct = $key.$_FILES['product_image']['name'][$key];
            $imgSizeProduct =$_FILES['product_image']['size'][$key];
            $tmp_dirProduct =$_FILES['product_image']['tmp_name'][$key];
            $upload_dirProduct = 'product_images/';
            $imgExtProduct = strtolower(pathinfo($imgFileProduct,PATHINFO_EXTENSION));
            $valid_extensionsProduct = array('jpeg', 'jpg', 'png', 'gif');
            $productpic= rand(1000,1000000).".".$imgExtProduct;
            {
            if(in_array($imgExtProduct, $valid_extensionsProduct)){         
                if($imgSizeProduct < 5000000){
                    move_uploaded_file($tmp_dirProduct,$upload_dirProduct.$productpic);
                }
                else{
                    $errMSG = "Sorry, your file is too large.";
                }
            }
            else{
                $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed2.";   
            }
        if(!isset($errMSG)){
            $stmt1 = $DB_con->prepare('INSERT INTO id_images(name_pic) VALUES(:uproductpic)');
            $stmt1->bindParam(':uproductpic',$productpic);
            if($stmt1->execute()){
                $successMSG = "new record succesfully inserted ...";
                header("refresh:5;index.php"); // redirects image view page after 5 seconds.
                $last_id = $DB_con->lastInsertId();
                echo "New record created successfully. Last inserted ID is: " . $last_id;
            }
            else{
                $errMSG = "error while inserting....";
            }
        }
    }
            }

        }

    }
    }

?>

Upvotes: 1

Views: 131

Answers (1)

marmeladze
marmeladze

Reputation: 6564

A quick fix might be,

$product_images_count = sizeof($_FILES['product_image']);

for($i=0; $i<$product_images_count; $i++) {
    $ProductimgFile = $_FILES['product_image'][$i]['name'];
    $Producttmp_dir = $_FILES['product_image'][$i]['tmp_name'];
    $ProductimgSize = $_FILES['product_image'][$i]['size'];
    do_something_with_those_variables(); 
}

Upvotes: 1

Related Questions