<?php
//var_dump ($_POST);
if(isset($_FILES["fileToUpload"])) {
    echo '<pre>';
    $dbh = new PDO('mysql:host=localhost;dbname=test', 'root', '', array( PDO::ATTR_PERSISTENT => false));
    $target_file = "test.zip";
    move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
    $zip = new ZipArchive;
    $res = $zip->open('test.zip');
    if ($res === TRUE) {
        echo 'ok';
        $zip->extractTo('test');
        $zip->close();  
        foreach(scandir('test') as $file){ 
          
          if(substr($file,0,1)=='.') continue;   
          echo $file."\n";
          $file1 = substr($file, 0, -4);
          $stmt = $dbh->prepare("DROP TABLE proracun_".$file1);
          $stmt->execute(); 
        $file = file("test/".$file);
        $cnt=0; 
        $pro = false; 
        $ins = '';
        $sql='';
        foreach($file as $red){ 
          if($pro) {
            $sql .= $red;
            $cnt++;
            if($cnt==200) {  //echo substr($sql, 0,-2);
              $stmt = $dbh->prepare(substr($sql, 0,-2));
              $stmt->execute();
              $cnt=0;
              $sql = $ins; 
            }
          } else {
            if(substr($red,0,11)!='INSERT INTO')$sql .= $red;
          }
            
           
          if(!$pro and substr($red,0,11)=='INSERT INTO'){ 

          try{
            $stmt = $dbh->prepare($sql);
            $stmt->execute();   
          } catch (PDOException $e) {
            echo "some fail-messages";
            }
          
            $ins = $red; 
            $sql = $ins;
            $pro = true;
          
          }
        } 
        if($cnt > 0) {
          $stmt = $dbh->prepare(substr($sql, 0,-2));
          $stmt->execute();
        }                              
          
        } 
        $stmt = $dbh->prepare("DROP TABLE proracun_konti");
        $stmt->execute();
        $stmt = $dbh->prepare("CREATE TABLE proracun_konti AS SELECT * FROM prov_konti;");
        $stmt->execute(); 
        $stmt = $dbh->prepare("DROP TABLE proracun_data");
        $stmt->execute();                            
        $stmt = $dbh->prepare("CREATE TABLE proracun_data AS SELECT * FROM prov_data;");
        $stmt->execute();
        echo '</pre>';
    } else {
        echo 'failed, code:' . $res;
    }
} else {
?>
<form method="POST" action="" enctype="multipart/form-data">
  <input type="file" name="fileToUpload" id="fileToUpload">
  <input type="submit" name="submit" />
</form> 
<?php  
}