Advertisement
Guest User

Untitled

a guest
Feb 28th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.37 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. $host     = 'localhost';
  5. $username = 'root';
  6. $password = '';
  7. $database = 'peter';
  8.  
  9. $db = new mysqli('localhost','root','','tst');
  10.  
  11. include('../../repair/php/fungsi.php');
  12. if(!isset($_SESSION['upload'])){
  13.     echo 'File was not uploaded!';
  14.     exit;
  15. }
  16. if(!isset($_SESSION['import_num'])){
  17.     if(file_exists('db')) recursiveDelete('db');
  18.     // if(file_exists('db.zip')) unlink('db.zip');
  19.     if(file_exists('db.zip')){
  20.         $archive = 'db.zip';
  21.         $zip = new ZipArchive;
  22.         $extracted = 'db';
  23.  
  24.         $open = $zip->open($archive);
  25.         if($open === true){
  26.             $zip->extractTo($extracted);
  27.             $zip->close();
  28.             unlink($archive);
  29.         }
  30.     }
  31.     if(file_exists('db')){
  32.         $a = $db->query("select database() s") or die($db->error);
  33.         $ad = $a->fetch_assoc();
  34.         $mydatabase = $ad['s'];
  35.         // drop
  36.         $q1 = $db->query("show tables") or die($db->error);
  37.         while($qd = $q1->fetch_assoc()){
  38.             $table = $qd['Tables_in_'.$mydatabase];
  39.             $drop = $db->query("drop table `$table`") or die($db->error);
  40.         }
  41.  
  42.  
  43.         $links = array();
  44.         foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator('db/', FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $path){
  45.             if($path->isFile()){
  46.                 $links[] = $path;
  47.             }
  48.         }
  49.         $_SESSION['import_num'] = count($links);
  50.         $_SESSION['db_files'] = $links;
  51.         $sql = file_get_contents('db/tables.sql');
  52.         flush_multi_query($sql);
  53.         unlink('db/tables.sql');
  54.         $spos = array_search('db/tables.sql',$_SESSION['db_files']);
  55.         unset($_SESSION['db_files'][$spos]);
  56.         $rf = count($_SESSION['db_files']);
  57.         $ja = '[{"status":"next set of records!","files":"'. $rf .'","num":"'. $_SESSION['import_num'] .'"}]';
  58.         echo $ja;
  59.     }
  60. }
  61. else{
  62.     /*$status = "";
  63.     $rf = count($_SESSION['db_files']);
  64.  
  65.     $fl = $_SESSION['db_files'][0];
  66.     $content = file_get_contents($fl);
  67.     $ar = explode('/',$fl);
  68.     $ls = $ar[count($ar) - 1];
  69.     $a2 = explode('.',$ar);
  70.     if(!isset($a2[1])){
  71.         $status = "An error occured!";
  72.     }
  73.     else{
  74.         $table = $ar[count($ar) - 2];
  75.  
  76.         $d = $db->query("show columns from `$table`") or die($db->error);
  77.         $counter = 0;
  78.         $cols = '';
  79.         $update = '';
  80.         while($dd = $d->fetch_assoc()){
  81.  
  82.             if($counter == 0){
  83.                 $cols = '`'.$dd['Field'].'`';
  84.                 $update = '`'.$dd['Field'].'` = values(`'.$dd['Field'].'`)';
  85.             }
  86.             else{
  87.                 $update = ',`'.$dd['Field'].'` = values(`'.$dd['Field'].'`)';
  88.                 $cols = ', `'.$dd['Field'].'`';
  89.             }
  90.             $counter++;
  91.         }
  92.  
  93.         // $columns = '('.$cols.')';
  94.  
  95.         $sql = "insert into `$table` ($cols) ".$content." on duplicate key update $update";
  96.         flush_multi_query($sql);
  97.         $status = "next set of records!";
  98.  
  99.         $spos = array_search($fl,$_SESSION['db_files']);
  100.         unset($_SESSION['db_files'][$spos]);
  101.         unlink($fl);
  102.     }
  103.     $rf = count($_SESSION['db_files']);
  104.     if($rf == 0) $status = "successfully imported!";
  105.     $ja = '[{"status":"'.$status.'","files":"'. $rf .'","num":"'. $_SESSION['import_num'] .'"}]';*/
  106.     echo 'abc';
  107. }
  108. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement