Guest User

Untitled

a guest
Sep 18th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. <?php
  2. $source = "inventory.csv";
  3. $target = fopen("inventory.csv", "w");
  4. $conn = ftp_connect("ftp.myserver.com") or die("Could not connect");
  5. ftp_login($conn,"Inventory","NurjVubs");
  6. ftp_fget($conn,$target,$source,FTP_ASCII);
  7. echo "file downloaded.n";
  8. /********************************/
  9. /* Edit the entries below to reflect the appropriate values
  10. /********************************/
  11. $dbhost = "localhost";
  12. $dbname = "mydb";
  13. $dbtable = "invent";
  14. $dbusername ="root";
  15. $dbpassword = "mypass";
  16. $fieldseparator = ",";
  17. $lineseparator = "n";
  18. $csvfile = "inventory.csv";
  19. /********************************/
  20. /* Would you like to add an ampty field at the beginning of these records?
  21. /* This is useful if you have a table with the first field being an auto_increment
  22. /* integer and the csv file does not have such as empty field before the records.
  23. /* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
  24. /* This can dump data in the wrong fields if this extra field does not
  25. /* exist in the table.
  26. /********************************/
  27. $addauto = 0;
  28. /********************************/
  29. /* Would you like to save the mysql queries in a file? If yes set $save to 1.
  30. /* Permission on the file should be set to 777. Either upload a sample file
  31. /* through ftp and change the permissions, or execute at the prompt:
  32. /* touch output.sql && chmod 777 output.sql
  33. /********************************/
  34. $save = 0;
  35. $outputfile = "output.sql";
  36. /********************************/
  37. if(!file_exists($csvfile)) {
  38. echo "File not found. Make sure you specified the correct path.n";
  39. exit;
  40. }
  41. $file = fopen($csvfile,"r");
  42. if(!$file) {
  43. echo "Error opening data file.n";
  44. exit;
  45. }
  46. $size = filesize($csvfile);
  47. if(!$size) {
  48. echo "File is empty.n";
  49. exit;
  50. }
  51. $csvcontent = fread($file,$size);
  52. fclose($file);
  53. $con = @mysql_connect($dbhost,$dbusername,$dbpassword) or die(mysql_error());
  54. @mysql_select_db($dbname) or die(mysql_error());
  55. $lines = 0;
  56. $queries = "";
  57. $linearray = array();
  58. foreach(split($lineseparator,$csvcontent) as $line) {
  59. $lines++;
  60. $line = trim($line," t");
  61. $line = str_replace("r","",$line);
  62. /************************************
  63. /* This line escapes the special character.
  64. /* Remove it if entries are already escaped in the csv file
  65. /************************************/
  66. $line = str_replace("'","'",$line);
  67. /*************************************/
  68. $linearray = explode($fieldseparator,$line);
  69. $linemysql = implode("','",$linearray);
  70. if($addauto) {
  71. $query = "insert into $dbtable values('','$linemysql');";
  72. }
  73. else {
  74. $query = "insert into $dbtable values('$linemysql');";
  75. }
  76. $queries .= $query . "n";
  77. @mysql_query($query);
  78. }
  79. @mysql_close($con);
  80. if($save) {
  81. if(!is_writable($outputfile)) {
  82. echo "File is not writable, check permissions.n";
  83. }
  84. else {
  85. $file2 = fopen($outputfile,"w");
  86. if(!$file2) {
  87. echo "Error writing to the output file.n";
  88. }
  89. else {
  90. fwrite($file2,$queries);
  91. fclose($file2);
  92. }
  93. }
  94. }
  95. echo "Found a total of $lines records in this csv file.n";
  96. $source = "inventory.csv";
  97. $target = fopen("inventory.csv", "w");
  98. $conn = ftp_connect("ftp.myserver.com") or die("Could not connect");
  99. ftp_login($conn,"Inventory","NurjVubs");
  100. ftp_delete($conn,$source);
  101. ftp_close($conn);
  102. echo "file deleted";
  103. mysql_close($con);
  104. ?>
Add Comment
Please, Sign In to add comment