Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Import załącznika z e-maila do bazy danych, prośba o sprawdzenie kodu
ufo1990
post 26.01.2022, 16:43:54
Post #1





Grupa: Zarejestrowani
Postów: 83
Pomógł: 0
Dołączył: 26.07.2019

Ostrzeżenie: (0%)
-----


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. ?>
Go to the top of the page
+Quote Post
nospor
post 26.01.2022, 16:52:38
Post #2





Grupa: Moderatorzy
Postów: 36 446
Pomógł: 6292
Dołączył: 27.12.2004




Ten csv ma tylko jeden wiersz?


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
ufo1990
post 26.01.2022, 17:50:18
Post #3





Grupa: Zarejestrowani
Postów: 83
Pomógł: 0
Dołączył: 26.07.2019

Ostrzeżenie: (0%)
-----


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);


Ten post edytował ufo1990 26.01.2022, 17:54:11
Go to the top of the page
+Quote Post
nospor
post 26.01.2022, 17:51:43
Post #4





Grupa: Moderatorzy
Postów: 36 446
Pomógł: 6292
Dołączył: 27.12.2004




Nie wnikam czy to txt czy csv. Pytam sie czy ma tylko jeden wiersz?


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
ufo1990
post 26.01.2022, 17:54:35
Post #5





Grupa: Zarejestrowani
Postów: 83
Pomógł: 0
Dołączył: 26.07.2019

Ostrzeżenie: (0%)
-----


Plik czasem ma nawet 30 lini
Go to the top of the page
+Quote Post
nospor
post 26.01.2022, 17:56:07
Post #6





Grupa: Moderatorzy
Postów: 36 446
Pomógł: 6292
Dołączył: 27.12.2004




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


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
ufo1990
post 26.01.2022, 18:01:17
Post #7





Grupa: Zarejestrowani
Postów: 83
Pomógł: 0
Dołączył: 26.07.2019

Ostrzeżenie: (0%)
-----


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');


Ten post edytował ufo1990 26.01.2022, 18:02:23
Go to the top of the page
+Quote Post
nospor
post 26.01.2022, 18:02:51
Post #8





Grupa: Moderatorzy
Postów: 36 446
Pomógł: 6292
Dołączył: 27.12.2004




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


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
ufo1990
post 26.01.2022, 18:10:54
Post #9





Grupa: Zarejestrowani
Postów: 83
Pomógł: 0
Dołączył: 26.07.2019

Ostrzeżenie: (0%)
-----


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. }
Go to the top of the page
+Quote Post
nospor
post 26.01.2022, 18:12:53
Post #10





Grupa: Moderatorzy
Postów: 36 446
Pomógł: 6292
Dołączył: 27.12.2004




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) {
.....


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
ufo1990
post 26.01.2022, 18:14:36
Post #11





Grupa: Zarejestrowani
Postów: 83
Pomógł: 0
Dołączył: 26.07.2019

Ostrzeżenie: (0%)
-----


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);
Go to the top of the page
+Quote Post
nospor
post 26.01.2022, 18:19:12
Post #12





Grupa: Moderatorzy
Postów: 36 446
Pomógł: 6292
Dołączył: 27.12.2004




fclose($filename_save);
To powinno byc po zapisie a nie hen po


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
ufo1990
post 26.01.2022, 18:28:03
Post #13





Grupa: Zarejestrowani
Postów: 83
Pomógł: 0
Dołączył: 26.07.2019

Ostrzeżenie: (0%)
-----


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. }
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.04.2024 - 00:39