Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Import załącznika z e-maila do bazy danych
Forum PHP.pl > Forum > PHP
ufo1990
Co dziennie otrzymuje na skrzynkę e-mail wiadomość z załącznikiem, który do tej pory pobierałem i ręcznie importowałem do programu. Zrobiłem skrypt, który pobiera załącznik ze skrzynki a następnie wpisuje dane zawarte w załączniku do bazy danych. Kod ogólnie działa jednak proszę o informacje czy wszystko jest zrobione tak jak trzeba, trochę nie daje mi spokoju czy dobrze zrobiłem że dwa razy występuje


  1. <?php
  2. $hostname = "{XXXXXXX:993/imap/ssl/novalidate-cert}INBOX";
  3. $username = XXXXXXXXX";
  4. $password = "XXXXXXXXX";
  5.  
  6. $inbox = imap_open($hostname,$username,$password);
  7. $emails = imap_search($inbox,'ALL');
  8.  
  9. if($emails)
  10. {
  11. $count = 1;
  12. foreach($emails as $email_number)
  13. {
  14. $overview = imap_fetch_overview($inbox,$email_number,0);
  15. $message = imap_fetchbody($inbox,$email_number,2);
  16. $structure = imap_fetchstructure($inbox, $email_number);
  17. $attachments = array();
  18.  
  19. if(isset($structure->parts) && count($structure->parts))
  20. {
  21. for($i = 0; $i < count($structure->parts); $i++)
  22. {
  23. $attachments[$i] = array(
  24. 'is_attachment' => false,
  25. 'filename' => '',
  26. 'name' => '',
  27. 'attachment' => ''
  28. );
  29.  
  30. if($structure->parts[$i]->ifdparameters)
  31. {
  32. foreach($structure->parts[$i]->dparameters as $object)
  33. {
  34. if(strtolower($object->attribute) == 'filename')
  35. {
  36. $attachments[$i]['is_attachment'] = true;
  37. $attachments[$i]['filename'] = $object->value;
  38. }
  39. }
  40. }
  41.  
  42. if($structure->parts[$i]->ifparameters)
  43. {
  44. foreach($structure->parts[$i]->parameters as $object)
  45. {
  46. if(strtolower($object->attribute) == 'name')
  47. {
  48. $attachments[$i]['is_attachment'] = true;
  49. $attachments[$i]['name'] = $object->value;
  50. }
  51. }
  52. }
  53.  
  54. if($attachments[$i]['is_attachment'])
  55. {
  56. $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
  57.  
  58. if($structure->parts[$i]->encoding == 3)
  59. {
  60. $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
  61. }
  62. else
  63. {
  64. if($structure->parts[$i]->encoding == 4)
  65. {
  66. $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
  67. }
  68. }
  69. }
  70. }
  71. }
  72.  
  73. require_once "connect.php";
  74. $connect = @new mysqli($host, $db_user, $db_password, $db_name);
  75.  
  76. if($connect->connect_error)
  77. {
  78. throw new Exception(mysqli_connect_errno());
  79. }
  80.  
  81. foreach($attachments as $attachment)
  82. {
  83. if($attachment['is_attachment'] == 1)
  84. {
  85. $filename = $attachment['name'];
  86. $filename_save = fopen($filename, "w+");
  87. $file = fwrite($filename_save, $attachment['attachment']);
  88.  
  89. if(isset($file))
  90. {
  91. ini_set('auto_detect_line_endings', TRUE);
  92. $filename_open = fopen($filename, "r");
  93.  
  94. while(($emapData = fgetcsv($filename_open, 10000, " ")) !== FALSE)
  95. {
  96. substr($emapData[1], 0, 4) .'-'. substr($emapData[1], 4,2) .'-'. substr($emapData[1],6,2);
  97. if($connect->query("INSERT into employees (`id_employee`, `sap_number`, `startdate`, `name`,`lastname`,`status`,`anonymization`) values(null,'$emapData[0]','$emapData[1]','$emapData[3]','$emapData[2]','','0')"))
  98. {
  99. unlink($filename);
  100. header('Location: employees.php');
  101. }
  102. else
  103. {
  104. throw new Exception(mysqli_connect_errno());
  105. }
  106. }
  107. fclose($filename_open);
  108. }
  109. fclose($filename_save);
  110. }
  111. }
  112. }
  113. }
  114. imap_delete($inbox,'1:*');
  115. imap_expunge($inbox);
  116. imap_close($inbox);
  117. ?>
nospor
Ten csv ma tylko jeden wiersz?
ufo1990
Tak naprawdę jest to plik z rozszerzeniem txt. Poniższy kod odpowiada za sprawdzanie linii w pliku i to działa.
  1. ini_set('auto_detect_line_endings', TRUE);
nospor
Nie wnikam czy to txt czy csv. Pytam sie czy ma tylko jeden wiersz?
ufo1990
Plik czasem ma nawet 30 lini
nospor
To czemu o to:
unlink($filename);
header('Location: employees.php');
robisz po kazdej linii? Jesli plik ma 30 linii to ty to odpalasz 30 razy. plik juz dawno nie istnieje a naglowek powinno sie wyslac tylko raz. Przeciez to ma byc zrobione raz po zakonczeniu petli a nie co linia
ufo1990
Faktycznie, wyrzuciłem to po za pętle. Teraz powinno być chyba ok.

  1. while(($emapData = fgetcsv($filename_open, 10000, " ")) !== FALSE)
  2. {
  3. substr($emapData[1], 0, 4) .'-'. substr($emapData[1], 4,2) .'-'. substr($emapData[1],6,2);
  4. if(!$connect->query("INSERT into employees (`id_employee`, `sap_number`, `startdate`, `name`,`lastname`,`status`,`anonymization`) values(null,'$emapData[0]','$emapData[1]','$emapData[3]','$emapData[2]','','0')"))
  5. {
  6. throw new Exception(mysqli_connect_errno());
  7. }
  8. }
  9. unlink($filename);
  10. header('Location: employees.php');
nospor
TEraz zajrzyj do manuala i zobacz co zwraca fwrite a nastepnie zastanow sie czy ten kod
$file = fwrite($filename_save, $attachment['attachment']);

if(isset($file))
A konkrennie IF, ma jakikowliek sens
ufo1990
ok, kolejna rzecz uporządkowana.
  1. if($attachment['is_attachment'] == 1)
  2. {
  3. $filename = $attachment['name'];
  4. $filename_save = fopen($filename, "w+");
  5. fwrite($filename_save, $attachment['attachment']);
  6.  
  7. ini_set('auto_detect_line_endings', TRUE);
  8. $filename_open = fopen($filename, "r");
  9.  
  10. while(($emapData = fgetcsv($filename_open, 10000, " ")) !== FALSE)
  11. {
  12. substr($emapData[1], 0, 4) .'-'. substr($emapData[1], 4,2) .'-'. substr($emapData[1],6,2);
  13. if(!$connect->query("INSERT into employees (`id_employee`, `sap_number`, `startdate`, `name`,`lastname`,`status`,`anonymization`) values(null,'$emapData[0]','$emapData[1]','$emapData[3]','$emapData[2]','','0')"))
  14. {
  15. throw new Exception(mysqli_connect_errno());
  16. }
  17. }
  18. unlink($filename);
  19. header('Location: employees.php');
  20. fclose($filename_open);
  21. fclose($filename_save);
  22. }
  23. }
nospor
Nie do konca uporzadkowana. Jedyne co zrobiles to wywaliles bezsensowny IF z isset ale tam powinien byc IF, ino ze bez isset. Przeciez tam moze byc FALSE..

$file = fwrite($filename_save, $attachment['attachment']);
if($file) {
.....
ufo1990
ok, poprawione.
  1. if($file)
  2. {
  3. ini_set('auto_detect_line_endings', TRUE);
  4. $filename_open = fopen($filename, "r");
  5. while(($emapData = fgetcsv($filename_open, 10000, " ")) !== FALSE)
  6. {
  7. substr($emapData[1], 0, 4) .'-'. substr($emapData[1], 4,2) .'-'. substr($emapData[1],6,2);
  8. if(!$connect->query("INSERT into employees (`id_employee`, `sap_number`, `startdate`, `name`,`lastname`,`status`,`anonymization`) values(null,'$emapData[0]','$emapData[1]','$emapData[3]','$emapData[2]','','0')"))
  9. {
  10. throw new Exception(mysqli_connect_errno());
  11. }
  12. }
  13. unlink($filename);
  14. header('Location: employees.php');
  15. fclose($filename_open);
  16. }
  17. fclose($filename_save);
nospor
fclose($filename_save);
To powinno byc po zapisie a nie hen po
ufo1990
Poprawione
  1. if($file)
  2. {
  3. ......................
  4. {
  5. .................
  6. }
  7. unlink($filename);
  8. header('Location: employees.php');
  9. fclose($filename_open);
  10. fclose($filename_save);
  11. }
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2024 Invision Power Services, Inc.