Guest User

Untitled

a guest
Jan 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. <?php
  2. header('Content-Type: text/html; charset=utf-8');
  3.  
  4.  
  5. /********************************/
  6. /* Code at http://legend.ws/blog/tips-tricks/csv-php-mysql-import/
  7. /* Edit the entries below to reflect the appropriate values
  8. /********************************/
  9.  
  10. /********************************/
  11. /* Would you like to add an ampty field at the beginning of these records?
  12. /* This is useful if you have a table with the first field being an auto_increment integer
  13. /* and the csv file does not have such as empty field before the records.
  14. /* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
  15. /* This can dump data in the wrong fields if this extra field does not exist in the table
  16. /********************************/
  17. $addauto = 1;
  18. /********************************/
  19. /* Would you like to save the mysql queries in a file? If yes set $save to 1.
  20. /* Permission on the file should be set to 777. Either upload a sample file through ftp and
  21. /* change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql
  22. /********************************/
  23. $save = 1;
  24. $outputfile = "output.sql";
  25. $csvfile = "test.csv";
  26. /********************************/
  27.  
  28.  
  29. if(!file_exists($csvfile)) {
  30. echo $csvfile . ": File not found. Make sure you specified the correct path.n";
  31. exit;
  32. }
  33.  
  34. $file = fopen($csvfile,"r");
  35.  
  36. if(!$file) {
  37. echo "Error opening data file.n";
  38. exit;
  39. }
  40.  
  41. $size = filesize($csvfile);
  42.  
  43. if(!$size) {
  44. echo "<p class="error">File is empty.</p>n";
  45. exit;
  46. }
  47.  
  48. $csvcontent = fread($file,$size);
  49.  
  50. fclose($file);
  51.  
  52. $con = @mysqli_connect($databasehost,$databaseusername,$databasepassword) or die(mysqli_error());
  53. mysqli_query("SET character_set_results=utf8", $con);
  54. mb_language('uni');
  55. mb_internal_encoding('UTF-8');
  56. mysqli_query("set names 'utf8'",$con);
  57.  
  58. @mysqli_select_db($databasename) or die(mysqli_error());
  59. @mysqli_query("truncate $databasetable");
  60. $lines = 0;
  61. $queries = "";
  62. $linearray = array();
  63.  
  64. foreach(split($lineseparator,$csvcontent) as $line) {
  65.  
  66. $lines++;
  67.  
  68. $line = trim($line," n");
  69.  
  70. $line = str_replace("r","",$line);
  71. $line = str_replace(""","",$line);
  72.  
  73. /*get COLUMN NAMEs from first line of CSV */
  74. if($lines==0 || $lines==1) {
  75. $columns=explode($fieldseparator,$line);
  76.  
  77. $columnsql=implode(",",$columns);
  78.  
  79. continue;
  80. }
  81.  
  82.  
  83.  
  84. /************************************
  85. This line escapes the special character. remove it if entries are already escaped in the csv file
  86. ************************************/
  87. $line = str_replace("'","'",$line);
  88. /*************************************/
  89.  
  90. $linearray = explode($fieldseparator,$line);
  91.  
  92. $linemysql = implode("','",$linearray);
  93.  
  94.  
  95. if($addauto)
  96. $query = "insert into $databasetable values('','$linemysql');";
  97. else
  98. $query = "insert into $databasetable values('$linemysql');";
  99.  
  100. $queries .= $query . "n";
  101.  
  102. @mysqli_query($query);
  103. }
  104.  
  105. @mysqli_close($con);
  106.  
  107. if($save) {
  108.  
  109. if(!is_writable($outputfile)) {
  110. echo "File is not writable, check permissions.n";
  111. }
  112.  
  113. else {
  114. $file2 = fopen($outputfile,"w");
  115.  
  116. if(!$file2) {
  117. echo "Error writing to the output file.n";
  118. }
  119. else {
  120. fwrite($file2,$queries);
  121. fclose($file2);
  122. }
  123. }
  124.  
  125. }
  126.  
  127. echo "Found a total of $lines records in this csv file.n";
  128.  
  129.  
  130. ?>
Add Comment
Please, Sign In to add comment