Advertisement
cahyadsn

import csv to mysql

Nov 7th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.46 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <title>IMPORT CSV FILE TO MYSQL</title>
  5.   </head>
  6.   <body>
  7.     <form method='post' enctype='multipart/form-data'>
  8.       <input type="file" name="file" />
  9.       <input type="submit" name="importFile" value="IMPORT">
  10.     </form>
  11.   </body>
  12. </html>
  13. <?php
  14. if(isset($_POST['importFile'])){
  15.   $MIME_types = array(
  16.                       'text/x-comma-separated-values',
  17.                       'text/comma-separated-values',
  18.                       'application/octet-stream',
  19.                       'application/vnd.ms-excel',
  20.                       'application/x-csv',
  21.                       'text/x-csv',
  22.                       'text/csv',
  23.                       'application/csv',
  24.                       'application/excel',
  25.                       'application/vnd.msexcel',
  26.                       'text/plain'
  27.                      );
  28.   if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$MIME_types)){
  29.         if(is_uploaded_file($_FILES['file']['tmp_name'])){
  30.            $bhost='localhost';
  31.            $dbuser='root';
  32.            $dbpass='';
  33.            $dbname='test';
  34.            $db=new mysqli($dbhist,$dbuser,$dbpass,$dbname);
  35.            $sql="LOAD DATA LOCAL INFILE '{$_FILE['file']['tmp_name']}' INTO TABLE tablename FIELDS TERMINATED by ',' LINES TERMINATED BY '\n' ";
  36.            $db->query($sql) or die("Fail to import data file to db, error number [".mysqli_errno."] : ".mysqli_error);
  37.         }
  38.   }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement