<?php include 'topmost.php';

// Count total files
$countfiles = count($_FILES['files']['name']);
$where =$_POST['where'];
$prevImg = $_POST['prevImg'];

if (isset($_FILES['files'])) {$countfiles = count($_FILES['files']['name']);}
$upload_location = "../../products/";
$files_arr = array();
for($index = 0;$index < $countfiles;$index++){
	if(isset($_FILES['files']['name'][$index]) && $_FILES['files']['name'][$index] != ''){
    	$filename = $_FILES['files']['name'][$index];
	
        $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
        $valid_ext = array("png","jpeg","jpg");
        if(in_array($ext, $valid_ext)){ $filename = rand().str_replace(' ','_',$filename);

        $path = $upload_location.$filename; 

            // Upload file
    		if(move_uploaded_file($_FILES['files']['tmp_name'][$index],$path)){
                $files_arr[] = $path;
        smart_resize_image(null , file_get_contents($path), '600' , '600' , false , $path , false , false ,100 );
    		}
        }
    }
			   	
}
//save to DB
if($prevImg!=''){
    $allImg = implode(',',array_merge(explode(',',$prevImg),$files_arr));
}else{ $allImg=implode(',',$files_arr); }
QueryDB("UPDATE products SET item_img ='$allImg' where id='$where' ");
echo implode(',',$files_arr);

?>





