Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // UPLOAD CSV CUSTOM
- // Ver. 1.0
- // by Jawaad
- // zaibatsu@gmail.com
- if(!defined('jin')) { die('nice try..'); }
- if($_POST[upload_csv]=="1") {
- $table_name="`".$_POST[table_name]."`";
- if(!$_FILES["file_csv"]["name"]) {
- echo "<script>alert('Please Browse File.. CSV.');</script>";
- } else if(substr($_FILES["file_csv"]["name"], -3) != "csv") {
- echo "<script>alert('File ".substr($_FILES["file_csv"]["name"],-3)." not support. Please use CSV extension');</script>";
- } else if($_FILES["file_csv"]["size"]>=1672864) {
- echo "<script>alert('File size is too big. File max 1.5 MB');</script>";
- } else {
- // READ FIELD FROM TABEL
- $sql_tabel = mysql_query("SELECT * FROM ".$table_name."");
- $numfields = mysql_num_fields($sql_tabel);
- $field_name = "";
- for ($i=0; $i < $numfields; $i++) {
- $get_field = mysql_field_name($sql_tabel, $i);
- if($i == 1) {
- $field_name .= "`".$get_field."`";
- } elseif($i > 1) {
- $field_name .= ", `".$get_field."`";
- }
- }
- copy($_FILES["file_csv"]["tmp_name"], "tmp/".$_FILES["file_csv"]["name"]);
- $ifile = fopen("tmp/".$_FILES["file_csv"]["name"],"r");
- $no=1;
- $jum_value = $numfields - 1;
- while (($file_list = fgetcsv($ifile, 1000, ",")) !== FALSE) {
- if($no > 1) {
- $field_value = "";
- for ($i=0; $i < $jum_value; $i++) {
- if($i == 0) {
- $field_value .= "'".$file_list[$i]."'";
- } else {
- $field_value .= ", '".$file_list[$i]."'";
- }
- }
- $sql = "INSERT INTO ".$table_name." (".$field_name.") VALUES (".$field_value.")";
- /*
- echo "<pre>";
- echo $sql;
- echo "</pre>";
- */
- $ex = mysql_query($sql) OR die ("<br>ERROR, DATA TIDAK BISA MASUK...<br>");
- if(!$ex) {
- echo "<script>alert(\"Sorry, Error....\");location.href='?do=upload_csv';</script>";
- } else {
- echo "<script>alert(\"Succesed.\");location.href='?do=upload_csv';</script>";
- }
- }
- $no++;
- }
- fclose($ifile);
- unlink("tmp/".$_FILES["file_csv"]["name"]) OR die ("Cannot DEL file tmp/".$_FILES["file_csv"]["name"]);
- }
- }
- ?>
- <form method="post" enctype="multipart/form-data">
- <br><br>
- <h2>Upload CSV</h2>
- <table border="0" cellspacing="0" cellpadding="3">
- <tr>
- <td valign="top">Table Name</td><td valign="top">: <input name="table_name"></td>
- </tr>
- <tr>
- <td valign="top">File CSV</td><td valign="top">: <input type="file" name="file_csv" value="1"></td>
- </tr>
- <tr>
- <td valign="top" colspan="2">
- <input type="hidden" name="upload_csv" value="1">
- <input type="submit" name="submit" value="UPLOAD .CSV">
- </td>
- </tr>
- </table>
- </form>
- <?php
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement