Advertisement
ujiajah1

uploader.php

Aug 23rd, 2016
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.63 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>Upload page</title>
  4. </head>
  5. <body>
  6. <?php
  7. //KONEKSI..
  8. $host='localhost:8080';
  9. $username='puji122';
  10. $password='bogor2016';
  11. $database='data';
  12. mysql_connect($host,$username,$password);
  13. mysql_select_db($database);
  14.  
  15. if (isset($_POST['submit'])) {//Script akan berjalan jika di tekan tombol submit..
  16.  
  17. //Script Upload File..
  18.     if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
  19.         echo "<h1>" . "File ". $_FILES['filename']['name'] ." Berhasil di Upload" . "</h1>";
  20.         echo "<h2>Menampilkan Hasil Upload:</h2>";
  21.         readfile($_FILES['filename']['tmp_name']);
  22.     }
  23.  
  24.     //Import uploaded file to Database, Letakan dibawah sini..
  25.     $handle = fopen($_FILES['filename']['tmp_name'], "r"); //Membuka file dan membacanya
  26.     while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  27.         $import="INSERT into data (id,nama,alamat) values(NULL,'$data[0]','$data[1]')"; //data array sesuaikan dengan jumlah kolom pada CSV anda mulai dari “0” bukan “1”
  28.         mysql_query($import) or die(mysql_error()); //Melakukan Import
  29.     }
  30.  
  31.     fclose($handle); //Menutup CSV file
  32.     echo "<br><strong>Import data selesai.</strong>";
  33.    
  34. }else { //Jika belum menekan tombol submit, form dibawah akan muncul.. ?>
  35.  
  36. <!-- Form Untuk Upload File CSV-->
  37.    Silahkan masukan file csv yang ingin diupload<br />
  38.    <form enctype='multipart/form-data' action='' method='post'>
  39.     Cari CSV File anda:<br />
  40.     <input type='file' name='filename' size='100'>
  41.    <input type='submit' name='submit' value='Upload'></form>
  42.  
  43. <?php } mysql_close(); //Menutup koneksi SQL?>
  44. </body>
  45. </html><br><br><br>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement