Advertisement
Guest User

Untitled

a guest
Apr 8th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. <?php
  2. header('Content-Type: text/html; charset=utf-8');
  3. $servername = "localhost";
  4. $username = "root";
  5. $password = "abc";
  6. $dbname = "project";
  7.  
  8. $conn = new mysqli($servername, $username, $password, $dbname);
  9.  
  10. if ($conn->connect_error) {
  11. die("Connection failed: " . $conn->connect_error);
  12. }
  13. $sql = "TRUNCATE TABLE datapack";
  14. mysqli_query($conn,$sql);
  15.  
  16. if(isset($_POST['submit']))
  17. {
  18. $fname = $_FILES['sel_file']['name'];
  19. echo 'upload file name: '.$fname.' ';
  20. $chk_ext = explode(".",$fname);
  21.  
  22. if(strtolower(end($chk_ext)) == "csv")
  23. {
  24. $filename = $_FILES['sel_file']['tmp_name'];
  25. $handle = fopen($filename, "r");
  26. $head = fgetcsv($handle);
  27. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
  28. {
  29. $sql= "INSERT into datapack(id,nickname,time,msg) values('$data[0]','$data[1]','$data[2]','$data[3]')";
  30. mysqli_query($conn,$sql);
  31. echo "Error: ". $conn->error;
  32. }
  33. echo "Success!";
  34. }
  35.  
  36. else
  37. {
  38. echo "Invalid File";
  39. }
  40. }
  41.  
  42. ?>
  43. <h1>Import CSV file</h1>
  44. <form action='<?php echo $_SERVER["PHP_SELF"];?>' method='post' enctype="multipart/form-data">
  45. Import File : <input type='file' name='sel_file' size='20'>
  46. <input type='submit' name='submit' value='submit'>
  47. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement