<?php
/**
 * Created by raziel.
 * User: raziel
 * Date: 25.12.2017
 * Time: 00:26
 */
error_reporting(E_ALL);
ini_set('display_errors', 1);



$con = mysqli_connect('localhost','dotrazie_new','catalonia9f532','dotrazie_new');

if(!$con){
    die("couldnt connect".mysqli_error);
}



extract($_POST);
$error=array();
$extension=array("jpeg","jpg","png","gif","JPG","PNG","JPEG","GIF");
$i = 1;
foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name)
{
    $file_name=$_FILES["files"]["name"][$key];
    $file_tmp=$_FILES["files"]["tmp_name"][$key];
    $ext=pathinfo($file_name,PATHINFO_EXTENSION);

    $txtGalleryName = $_POST['customer'];
    $imageType = $_POST['imageType'];


    if ($txtGalleryName == "elaborat") {
        $IdCust = 1;
    }

    if ($txtGalleryName == "noctis") {
        $IdCust = 2;
    }

    if ($txtGalleryName == "anna") {
        $IdCust = 3;
    }

    if ($txtGalleryName == "pretty") {
        $IdCust = 4;
    }

    if(!file_exists("assets/images/customers/".$txtGalleryName))
    {
        echo "FOLDER DOENS'T EXIST!</br>";
        mkdir("assets/images/customers/".$txtGalleryName, 0755);
        echo "FOLDER CREATED!</br>";
    }

    if(in_array($ext,$extension))
    {
        if(!file_exists("assets/images/customers/".$txtGalleryName."/".$file_name))
        {
            //$destinationIamge = 'assets/images/customers/';
            //$imageType = "asd";
            $destinationIamge = 'assets/images/customers/'.$txtGalleryName.'/'.$file_name;
            move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"assets/images/customers/".$txtGalleryName."/".$file_name);
            mysqli_query($con,"INSERT INTO pictures(IDCustomer,ImageType,Path) VALUES($IdCust,'$imageType','$destinationIamge')");
            //mysqli_query($con,"INSERT INTO pictures(IDCustomer,ImageType,Path) VALUES(1,2,3)");
            echo "Uploaded - $file_name"."!</br>";
            $i = $i + 1;
        }
        else
        {
            $filename=basename($file_name,$ext);
            $newFileName=$filename.time().".".$ext;
            move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"assets/images/customers/".$txtGalleryName."/".$newFileName);
        }
    }
    else
    {
        array_push($error,"$file_name, ");
    }
}


echo "

<script>
setTimeout(function() {
  
window.location.href = 'http://dotcodes.net/new/angular/new/arr.php';

}, 1000);
</script>

";





/*$target_dir = "assets/images/customers/anna/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 2500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
    && $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error

foreach($_FILES["fileToUpload"]["tmp_name"] as $key=>$tmp_name) {

if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

}*/

?>