Tutorial Programming

Tutorial membaca file CSV dengan mengunakan PHP


Hallo sahabat saya akan menjelaskan bagaimana cara membaca file CSV di PHP , nah kita langsung ke codingnya yah.

1.Buatla file file-csv.csv dengan isi

A1 . KODE;NAMA;JUMLAH
A2 . 1;BAJU KAOS;10
A3 . 2;KAOS KAKI;12
A4 . 3;SENDAL JEPIT;10

2. Copy coding tersebut.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contoh Baca dengan upload csv</title>
</head>
<body>
<?php
if (!empty($_FILES["filecsv"]["tmp_name"]))
{
 $namafile = $_FILES['filecsv']['tmp_name'];
 $pemisah=";";
 $datacsv = fopen($namafile, "r");
 while (($data = fgetcsv($datacsv, 1000, $pemisah)) !== FALSE)
 {
  $jml++;
  echo $data[0]." - ".$data[1]." - ".$data[2]."</br>";
 }
 fclose($datacsv); 
 echo "<SCRIPT>alert('".$jml." baris data berhasil dibaca');</SCRIPT>";
}
?>
<form action="" method="post" enctype="multipart/form-data" name="ContohUploadCSV" id="ContohUploadCSV">
  <p>Pilih file csv
    <input type="file" name="filecsv" id="filecsv" />
</p>
  <p>
    <input type="submit" name="button" id="button" value="Kirim" />
</p>
</form>
</body>
</html>

Sekian tutorial saya semoga bermanfaat.

Silakan download aplikasi disini

No comments:

Post a Comment

Tutorial Programming Designed by Templateism | Ary EJFA Copyright © 2015

Theme images by richcano. Powered by Blogger.