Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- header('Content-Type: text/html; charset=utf-8');
- $servername = "localhost";
- $username = "root";
- $password = "abc";
- $dbname = "project";
- $conn = new mysqli($servername, $username, $password, $dbname);
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $sql = "TRUNCATE TABLE datapack";
- mysqli_query($conn,$sql);
- if(isset($_POST['submit']))
- {
- $fname = $_FILES['sel_file']['name'];
- echo 'upload file name: '.$fname.' ';
- $chk_ext = explode(".",$fname);
- if(strtolower(end($chk_ext)) == "csv")
- {
- $filename = $_FILES['sel_file']['tmp_name'];
- $handle = fopen($filename, "r");
- $head = fgetcsv($handle);
- while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
- {
- $sql= "INSERT into datapack(id,nickname,time,msg) values('$data[0]','$data[1]','$data[2]','$data[3]')";
- mysqli_query($conn,$sql);
- echo "Error: ". $conn->error;
- }
- echo "Success!";
- }
- else
- {
- echo "Invalid File";
- }
- }
- ?>
- <h1>Import CSV file</h1>
- <form action='<?php echo $_SERVER["PHP_SELF"];?>' method='post' enctype="multipart/form-data">
- Import File : <input type='file' name='sel_file' size='20'>
- <input type='submit' name='submit' value='submit'>
- </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement