Item not being properly indexed in PHP array inside of for loop -
i have loop pulling attachments email using php's imap functions. works fine 1 attachment, unable pull more 1 attachment single mail. see code below -
for ($i = 0; $i < $count; $i++) { $attachments[$i] = array( 'is_attachment' => false ); if ($email['structure']->parts[$i]->ifdparameters) { foreach ($email['structure']->parts[$i]->dparameters $object) { if (strtolower($object->attribute) == 'filename') { $attachments[$i]['is_attachment'] = true; $attachments[$i]['filename'] = $object->value; } } } if ($email['structure']->parts[$i]->ifparameters) { foreach ($email['structure']->parts[$i]->parameters $object) { if (strtolower($object->attribute) == 'name') { $attachments[$i]['is_attachment'] = true; $attachments[$i]['name'] = $object->value; } } } if ($attachments[$i]['is_attachment']) { $attachments[$i]['attachment'] = imap_fetchbody($this->conn, $email['index'], $i+1); $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); } error_log(print_r($attachments[$i], true)); error_log(print_r($attachments, true)); }
it appears second attachment not being correctly added array's index. error logging shows value $attachment[$i] @ each value (0, 1, 2), however, $attachment contains 2 indexes (0, 1). first blank value (part of email), , second , third attachments. see sample error log output below -
[13-jun-2015 16:19:54 europe/berlin] array ( [is_attachment] => ) [13-jun-2015 16:19:54 europe/berlin] array ( [0] => array ([is_attachment] => )) [13-jun-2015 16:19:55 europe/berlin] array ( [is_attachment] => 1 [filename] => 2015-06-12_1048.png [name] => 2015-06-12_1048.png [attachment] => ‰png [13-jun-2015 16:19:55 europe/berlin] array ( [0] => array ( [is_attachment] => ) [1] => array ( [is_attachment] => 1 [filename] => 2015-06-12_1048.png [name] => 2015-06-12_1048.png [attachment] => ‰png [13-jun-2015 16:19:55 europe/berlin] array ( [is_attachment] => 1 [filename] => 5812548c-d445-4a18-a56d-e2698fdfd99b.jpg [name] => 5812548c-d445-4a18-a56d-e2698fdfd99b.jpg [attachment] => ÿØÿà [13-jun-2015 16:19:55 europe/berlin] array ( [0] => array ( [is_attachment] => ) [1] => array ( [is_attachment] => 1 [filename] => 2015-06-12_1048.png [name] => 2015-06-12_1048.png [attachment] => ‰png
as can see, third item, $attachments[2] array [attachment] => ÿØÿà, not added array when print $attachments. i'm @ loss here why shows when printing $attachments[$i] not $attachments. ideas?
update: error logging broken due binary files + null character. problem still exists can't pull more 1 attachment though. saving binaries in $attachments array disk, , 1 attachment saved, not both of them. why first binary saved?
alright, i'm posting answer then, question can marked answered:
this appears purely visual bug.
error logs provided seem incomplete, output after [attachment] =>
seems cut off (there should @ least closing brackets close array ( ...
, right?).
caused by
- those files containing null character (
\0
), rather binary files of length. - the error handler not using byte arrays, c-style strings, end indicated said null character.
so when logging data appears broken, when in fact, fine.
Comments
Post a Comment