3
votes
<input id="input-fcount-3" name="inputfcount3[]" multiple type="file" class="file-loading" accept="image/*">

First of all it has to validate the image counts minimum 02 and maximum 05, then file size maximum 8MB, after that image type "JPEG" and "PNG" only allowed and errors should be 0.

Then I have to create a folder as userName (logged in user's username it is in session it can be requested). inside that folder another folder should be created like userName_ID_220 (220 for this it has to select the last inserted id from Database SQL max function can be used). inside this folder image should move and rename like userName_id_220_0.jpg and next image should be like userName_id_220_1.jpg.

And I have to save all the image's paths inside database like userName/userName_ID_001/userName_id_220_0.jpg in column pic0 and other image path and name should be save in to column pic1 like userName/userName_ID_001/userName_id_220_1.jpg as shown in below picture.

DB sample Image

I tried foreach but it created each row for each image. so please suggest me a solution.

date_default_timezone_set("Asia");
$currentDateTime = date('Y-m-d H:i:s');

$ad_topic   = addslashes($_REQUEST['ad_topic']);
$distict    = $_REQUEST['distict'];
$city       = $_REQUEST['city'];
$price      = $_REQUEST['price'];
$details    = $_REQUEST['details'];
$user_name  = $_REQUEST['user_name'];
$mainCat    = addslashes($_REQUEST['Category']);
$subCat     = addslashes($_REQUEST['subCategory']);

if (isset($_REQUEST['item_brand']) && isset($_REQUEST['condition']) ) {
    $sub_sub_Cat = addslashes($_REQUEST['item_brand']);
    $condition   = $_REQUEST['condition'];

}else if(isset($_REQUEST['condition'])){
    $condition   = $_REQUEST['condition'];
}else if(isset($_REQUEST['item_brand'])){
    $sub_sub_Cat = addslashes($_REQUEST['item_brand']);
}else{

}

if (empty($ad_topic) || strlen($ad_topic)<=10) {
        echo "at least ad topic should be 10 characters long";
    }else if(empty($distict) || $distict=='Select District'){
        echo "Please Choose District";
    }else if(empty($city) || $city=='Select City'){
        echo "Please Choose City";
    }else if(empty($price)){
        echo "Please Enter Proper value for price";
    }else if(empty($details) || strlen($details)<=15 ){
        echo "At least details should be 15 characters long";
    }else if(empty($user_name)){
        echo "Something Wrong Contact Admin";
    }else if (empty($mainCat) || ($mainCat=='Select Category')) {
        echo "Please Choose Category";
    }else if (empty($subCat) || ($subCat=='Select Sub Category')) {
        echo "Please Choose Sub Category";
    }else if(isset($_REQUEST['item_brand']) && $sub_sub_Cat=='Select Item / Brand'){
        echo "Please Choose Item / Brand";
    }else if (isset($_REQUEST['condition']) && $condition=='Select Condition') {
        echo "Please Choose Condition";
    }else if(!isset($_FILES) || count($_FILES['inputfcount3']['name']) < 2){
        echo "Please choose at least 2 Pictures";
    }else{

    $totalPics = sizeof($_FILES['inputfcount3']['tmp_name']);

            if ($totalPics >= 2) {


                $file_type0 = ($_FILES['inputfcount3']['type'][0]) ;
                $file_type1 = ($_FILES['inputfcount3']['type'][1]) ;
                $file_type2 = ($_FILES['inputfcount3']['type'][2]) ;
                $file_type2 = ($_FILES['inputfcount3']['type'][3]) ;
                $file_type2 = ($_FILES['inputfcount3']['type'][4]) ;

                $file_size0 = ($_FILES['inputfcount3']['size'][0]) ;
                $file_size1 = ($_FILES['inputfcount3']['size'][1]) ;
                $file_size2 = ($_FILES['inputfcount3']['size'][2]) ;

                $file_error0    = ($_FILES['inputfcount3']['error'][0]) ;
                $file_error1    = ($_FILES['inputfcount3']['error'][1]) ;
                $file_error2    = ($_FILES['inputfcount3']['error'][2]) ;

            if ( ($file_type0 == "image/jpeg") || ($file_type0 == "image/png") && ($file_type1 == "image/jpeg") || ($file_type1 == "image/png") && ($file_type2 == "image/jpeg") || ($file_type2 == "image/png")&& ($file_error0 == 0) && ($file_error1 == 0) && ($file_error2 == 0)) {


                $sql = "INSERT INTO ads (user_name,ad_topic,district,city,price,condtition,details,status,date_time) VALUES (";
                $sql .= "'$user_name',";
                $sql .= "'$ad_topic',";
                $sql .= "'$distict',";
                $sql .= "'$city',";
                $sql .= "'$price',";
                $sql .= "'$condition',";
                $sql .= "'$details',";
                $sql .= "'waiting',";
                $sql .= "'$currentDateTime')";

                $q = mysql_query($sql,$link);

                $last_id = mysql_insert_id();


                    echo "\nAD Details added Successfully, Waiting for Admin Approval";

                    $folder_name = './assets/img/products/'.$user_name;
                    $sub_folder_name = $user_name.'_adid_'.$last_id;
                    $sub_folder_dir = $folder_name.'/'.$sub_folder_name;

                    if(!file_exists($folder_name)){

                        $create_folder =  mkdir('./assets/img/products/'.$user_name);

                        if (!file_exists($sub_folder_dir)) {
                            $create_sub_folder =  mkdir($folder_name.'/'.$sub_folder_name);

                        }else{

                            echo "\nsub folder exists if con folder name";
                        }



                    }else{
                        if (!file_exists($sub_folder_dir)) {
                            $create_sub_folder =  mkdir($folder_name.'/'.$sub_folder_name);

                                echo "\nDOWN HERE";
                        }else{

                            echo "\n sub folder exists if con folder name";
                        }
                    }


                    $file_tmp0  = ($_FILES['inputfcount3']['tmp_name'][0]) ;
                    $file_tmp1  = ($_FILES['inputfcount3']['tmp_name'][1]) ;
                    $file_tmp2  = ($_FILES['inputfcount3']['tmp_name'][2]) ;


                $paths=array($sub_folder_name.'_0.jpg',$sub_folder_name.'_1.jpg',$sub_folder_name.'_2.jpg');

                $destination0 = $folder_name.'/'.$sub_folder_name.'/'.$sub_folder_name."_0.jpg";                    
                $mov_pic = move_uploaded_file($file_tmp0,$destination0);
                PicResize($sub_folder_dir.'/',$sub_folder_name.'_0.jpg'); // $sub_folder_name Contains  $user_name.'_adid_'.$last_id line number 56

                $destination1 = $folder_name.'/'.$sub_folder_name.'/'.$sub_folder_name."_1.jpg";
                $mov_pic = move_uploaded_file($file_tmp1,$destination1);
                PicResize($sub_folder_dir.'/',$sub_folder_name.'_1.jpg');// $sub_folder_name Contains  $user_name.'_adid_'.$last_id line number 56

                $destination2 = $folder_name.'/'.$sub_folder_name.'/'.$sub_folder_name."_2.jpg";
                $mov_pic = move_uploaded_file($file_tmp2,$destination2);
                PicResize($sub_folder_dir.'/',$sub_folder_name.'_2.jpg');// $sub_folder_name Contains  $user_name.'_adid_'.$last_id line number 56
                if($mov_pic>0){

                    $pic0 = $user_name.'/'.$sub_folder_name.'/'.$sub_folder_name."_0.jpg";
                    $pic1 = $user_name.'/'.$sub_folder_name.'/'.$sub_folder_name."_1.jpg";
                    $pic2 = $user_name.'/'.$sub_folder_name.'/'.$sub_folder_name."_2.jpg";

                    $sqlPic = "UPDATE ads SET pic0='$pic0', pic1='$pic1', pic2='$pic2' WHERE adid='$last_id'";
                    $qry = mysql_query($sqlPic,$link);

                }


            }else{ // Ending IF condition of Image type, size, validations
                echo "\n Not Suitable Image";
        } 

        }
        else{

            echo "\n YOU HAVE UPLOADED $totalPics PICTURES, YOU MUST HAVE TO UPLOAD 02 PICTURES";
        }

}

I am facing some problems like if i uploaded 2 pics getting error undefined offset for $file_type2 = ($_FILES['inputfcount3']['type'][2]); how to fix that error and simplify this code?

1
Please, show us some code... So we can help you correct it - Salketer
Codes added please checkout - user466061

1 Answers

0
votes

Naturally it gives this error when you upload two pictures - $file_type2 is for the third picture which is not there. You have to change all $file_type to:

$file_type2 = (isset($_FILES['inputfcount3']['type'][2]) ? $_FILES['inputfcount3']['type'][2] : '') ;

Your $_FILES Array looks like this:

Array
(
[inputfcount3] => Array
    (
        [name] => Array
            (
                [0] => test1.jpg
                [1] => test2.png
            )

        [type] => Array
            (
                [0] => image/jpeg
                [1] => image/png
            )

        [tmp_name] => Array
            (
                [0] => /tmp/phpq62nsC
                [1] => /tmp/phpKTijeC
            )

        [error] => Array
            (
                [0] => 0
                [1] => 0
            )

        [size] => Array
            (
                [0] => 63722
                [1] => 3254
            )

    )

)

As I see it you don't need any foreach - just put your database file names together as you show it above. I though have one suggestion: use basename($_FILES['file_upload_int']['name']) for the filename.

basename

This way you don't overwrite the file ending with jgp even it is a png.

For security reasons I also suggest you use PDO instead of mysql functions and $_POST instead of $_REQUEST (in case you are not already sending your form per post).