drisate Posted March 2, 2009 Share Posted March 2, 2009 hey guys i made an attachement download code but for some reason instead of having image.jpg it gives image.jpg.html and it's invalide ... $mbox = imapOpen(); if (!$mbox) { $message = "This link is not valid anymore. Please login again."; die("<META HTTP-EQUIV='Refresh' CONTENT='0;url=?mod=mail&message=" . urlencode($message) . "'></META>"); } $msg = $_GET['num']; $bypasscheck = 0; if (!eregi("^([0-9]*)$", $msg) or !eregi("([0-9\.]*)", $_GET['part'])) exit(); $tmpa = array(); $ret = array(); if (eregi("([0-9\.]*)\.([0-9\.]*)", $_GET['part'], $ret)) { $pone = ($ret[1] - 1); $ptwo = ($ret[2] - 1); } else { $pone = ($_GET['part'] - 1); } $dpara = array(); $struct = @imap_fetchstructure($mbox, $msg); $body = @imap_fetchbody($mbox, $msg, $_GET['part']); @imap_close($mbox); $tmpa = $struct->parts; if (isset($ptwo)) { $tmpa = $tmpa[$pone]->parts; $obj = $tmpa[$ptwo]; } else { $obj = $tmpa[$pone]; } $dpara = $obj->dparameters; for ($v = 0; $v < sizeof($dpara); $v++) { if (eregi("filename", $dpara[$v]->attribute)) { $fname = $dpara[$v]->value; } } if (empty($fname)) { $para = $obj->parameters; for ($v = 0; $v < sizeof($para); $v++) { if (eregi("name", $para[$v]->attribute)) { $fname = $para[$v]->value; } } } if (empty($fname)) { $disp = $obj->description; } if (empty($fname)) { $fname = $lang_unknown; } $mime_type = mimetype($obj->type) . "/" . strtolower($obj->subtype); if ($mime_type == "message/rfc822") { $mime_type = "text/plain"; } header("Content-Type: " . $mime_type); switch ($obj->encoding) { case 4: $body = @imap_qprint($body); break; case 3: $body = @imap_base64($body); break; default: $beginning = substr($body, 0, 50); if (stristr($beginning, "BinHex") != false) $fname = $fname . ".binhex"; break; } header("Content-Disposition: attachment; filename=\"$fname\""); print $body; The link to access this looks like: index.php?mod=mail&CMD=Attachview&num=17&part=4 Quote Link to comment https://forums.phpfreaks.com/topic/147586-imap-open-attachements/ Share on other sites More sharing options...
drisate Posted March 2, 2009 Author Share Posted March 2, 2009 bump nobody? Quote Link to comment https://forums.phpfreaks.com/topic/147586-imap-open-attachements/#findComment-774821 Share on other sites More sharing options...
drisate Posted March 2, 2009 Author Share Posted March 2, 2009 Guys ... this is frustrating ... i completly remade my code ... and i have exacly the same problem ... what is it i don't get? Both of the codes are implemented in other applications and should be working ... function get_attached_file($mbox,$structure,$k,$mid) { $encoding = $structure->parts[$k]->encoding; //extract file name from headers $fileName = strtolower($structure->parts[$k]->dparameters[0]->value); //extract attachment from email body $fileSource = base64_decode(imap_fetchbody($mbox, $mid, $k+1)); //get extension $ext = substr($fileName, strrpos($fileName, '.') + 1); //die($ext); //get mime file type switch ($ext) { case "asf": $type = "video/x-ms-asf"; break; case "avi": $type = "video/avi"; break; case "flv": $type = "video/x-flv"; break; case "fla": $type = "application/octet-stream"; break; case "swf": $type = "application/x-shockwave-flash"; break; case "doc": $type = "application/msword"; break; case "docx": $type = "application/msword"; break; case "zip": $type = "application/zip"; break; case "xls": $type = "application/vnd.ms-excel"; break; case "gif": $type = "image/gif"; break; case "jpg" || "jpeg": $type = "image/jpg"; break; case "png": $type = "image/png"; break; case "wav": $type = "audio/wav"; break; case "mp3": $type = "audio/mpeg3"; break; case "mpg" || "mpeg": $type = "video/mpeg"; break; case "rtf": $type = "application/rtf"; break; case "htm" || "html": $type = "text/html"; break; case "xml": $type = "text/xml"; break; case "xsl": $type = "text/xsl"; break; case "css": $type = "text/css"; break; case "php": $type = "text/php"; break; case "txt": $type = "text/txt"; break; case "asp": $type = "text/asp"; break; case "pdf": $type = "application/pdf"; break; case "psd": $type = "application/octet-stream"; break; default: $type = "application/octet-stream"; } //download file header('Content-Description: File Transfer'); header('Content-Type: ' .$type); header('Content-Disposition: attachment; filename='.$fileName); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($fileSource)); ob_clean(); flush(); echo $fileSource; } $mbox = imapOpen(); if (!$mbox) { $message = "This link is not valid anymore. Please login again."; die("<META HTTP-EQUIV='Refresh' CONTENT='0;url=?mod=mail&message=" . urlencode($message) . "'></META>"); }else{ //$mbox = imap stream $mid = $_GET['num']; $partno = $_GET['part']-1; $structure = imap_fetchstructure($mbox, $mid); get_attached_file($mbox,$structure,$partno,$mid); } Quote Link to comment https://forums.phpfreaks.com/topic/147586-imap-open-attachements/#findComment-774841 Share on other sites More sharing options...
premiso Posted March 2, 2009 Share Posted March 2, 2009 Why not just replace .jpg.html with just .jpg ....quick solution to the problem. Have you done any debugging to see when/where the name gets changed to the .html? Quote Link to comment https://forums.phpfreaks.com/topic/147586-imap-open-attachements/#findComment-774844 Share on other sites More sharing options...
drisate Posted March 2, 2009 Author Share Posted March 2, 2009 I m not sure that's the best way to fix this... what if they are sending an html file? ... i don't know, sounds buggy. But why the hell is my code returning .html stuff? Quote Link to comment https://forums.phpfreaks.com/topic/147586-imap-open-attachements/#findComment-774870 Share on other sites More sharing options...
premiso Posted March 2, 2009 Share Posted March 2, 2009 Have you tried debugging it and see where the name gets skewed? If it is from the imap, chances are your mail server is doing it. If the file has .jpg.html chances are it is not suppose to be .html. A simple replace of that would suffice. Anyhow see where it gets changed, if it is that way from the get go. If it is, check with your mail service provider as they may know what is going on and what attachments are being returned as .html Quote Link to comment https://forums.phpfreaks.com/topic/147586-imap-open-attachements/#findComment-774872 Share on other sites More sharing options...
drisate Posted March 2, 2009 Author Share Posted March 2, 2009 Ok i got it to work ... but now it outputs an empty file $mbox = imapOpen(); if (!$mbox){ $message = "This link is not valid anymore. Please login again."; die("<META HTTP-EQUIV='Refresh' CONTENT='0;url=?mod=mail&message=".urlencode($message)."'></META>"); }else{ $mid = $_GET['num']; $k = $_GET['part']-1; $structure = imap_fetchstructure($mbox, $mid); $encoding = $structure->parts[$k]->encoding; //extract file name from headers $fileName = strtolower($structure->parts[$k]->dparameters[0]->value); //extract attachment from email body $fileSource = base64_decode(imap_fetchbody($mbox, $mid, $k+1)); //get extension $ext = substr($fileName, strrpos($fileName, '.') + 1); //get mime file type switch ($ext) { case "asf": $type = "video/x-ms-asf"; break; case "avi": $type = "video/avi"; break; case "flv": $type = "video/x-flv"; break; case "fla": $type = "application/octet-stream"; break; case "swf": $type = "application/x-shockwave-flash"; break; case "doc": $type = "application/msword"; break; case "docx": $type = "application/msword"; break; case "zip": $type = "application/zip"; break; case "xls": $type = "application/vnd.ms-excel"; break; case "gif": $type = "image/gif"; break; case "jpg" || "jpeg": $type = "image/jpg"; break; case "png": $type = "image/png"; break; case "wav": $type = "audio/wav"; break; case "mp3": $type = "audio/mpeg3"; break; case "mpg" || "mpeg": $type = "video/mpeg"; break; case "rtf": $type = "application/rtf"; break; case "htm" || "html": $type = "text/html"; break; case "xml": $type = "text/xml"; break; case "xsl": $type = "text/xsl"; break; case "css": $type = "text/css"; break; case "php": $type = "text/php"; break; case "txt": $type = "text/txt"; break; case "asp": $type = "text/asp"; break; case "pdf": $type = "application/pdf"; break; case "psd": $type = "application/octet-stream"; break; default: $type = "application/octet-stream"; } //download file header('Content-Description: File Transfer'); header('Content-Type: ' .$type); header('Content-Disposition: attachment; filename='.$fileName); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($fileSource)); ob_clean(); flush(); echo $fileSource; } this is a print_r on imap_fetchstructure stdClass Object ( [type] => 1 [encoding] => 0 [ifsubtype] => 1 [subtype] => MIXED [ifdescription] => 0 [ifid] => 0 [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] => 1 [parameters] => Array ( [0] => stdClass Object ( [attribute] => boundary [value] => 4-1410065408-1410065408=:56890 ) ) [parts] => Array ( [0] => stdClass Object ( [type] => 1 [encoding] => 0 [ifsubtype] => 1 [subtype] => ALTERNATIVE [ifdescription] => 0 [ifid] => 0 [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] => 1 [parameters] => Array ( [0] => stdClass Object ( [attribute] => boundary [value] => 6-1410065408-1410065408=:72275 ) ) [parts] => Array ( [0] => stdClass Object ( [type] => 0 [encoding] => 4 [ifsubtype] => 1 [subtype] => PLAIN [ifdescription] => 0 [ifid] => 0 [bytes] => 52 [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] => 1 [parameters] => Array ( [0] => stdClass Object ( [attribute] => charset [value] => windows-1256 ) ) ) [1] => stdClass Object ( [type] => 0 [encoding] => 4 [ifsubtype] => 1 [subtype] => HTML [ifdescription] => 0 [ifid] => 0 [lines] => 1 [bytes] => 161 [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] => 1 [parameters] => Array ( [0] => stdClass Object ( [attribute] => charset [value] => windows-1256 ) ) ) ) ) [1] => stdClass Object ( [type] => 5 [encoding] => 3 [ifsubtype] => 1 [subtype] => PJPEG [ifdescription] => 0 [ifid] => 0 [bytes] => 17588 [ifdisposition] => 1 [disposition] => attachment [ifdparameters] => 1 [dparameters] => Array ( [0] => stdClass Object ( [attribute] => filename [value] => drisate_1.jpg ) ) [ifparameters] => 1 [parameters] => Array ( [0] => stdClass Object ( [attribute] => name [value] => ) ) ) [2] => stdClass Object ( [type] => 5 [encoding] => 3 [ifsubtype] => 1 [subtype] => PJPEG [ifdescription] => 0 [ifid] => 0 [bytes] => 10596 [ifdisposition] => 1 [disposition] => attachment [ifdparameters] => 1 [dparameters] => Array ( [0] => stdClass Object ( [attribute] => filename [value] => avatar-63671.jpg ) ) [ifparameters] => 1 [parameters] => Array ( [0] => stdClass Object ( [attribute] => name [value] => ) ) ) [3] => stdClass Object ( [type] => 5 [encoding] => 3 [ifsubtype] => 1 [subtype] => PJPEG [ifdescription] => 0 [ifid] => 0 [bytes] => 55046 [ifdisposition] => 1 [disposition] => attachment [ifdparameters] => 1 [dparameters] => Array ( [0] => stdClass Object ( [attribute] => filename [value] => moi.jpg ) ) [ifparameters] => 1 [parameters] => Array ( [0] => stdClass Object ( [attribute] => name [value] => ) ) ) ) ) The page link looks like index.php?mod=mail&CMD=Attachview&num=17&part=3 so i am trying to download part 2 (works to get it but it's empty ..) Quote Link to comment https://forums.phpfreaks.com/topic/147586-imap-open-attachements/#findComment-774906 Share on other sites More sharing options...
drisate Posted March 2, 2009 Author Share Posted March 2, 2009 bump Quote Link to comment https://forums.phpfreaks.com/topic/147586-imap-open-attachements/#findComment-774918 Share on other sites More sharing options...
drisate Posted March 2, 2009 Author Share Posted March 2, 2009 bump anybody? Quote Link to comment https://forums.phpfreaks.com/topic/147586-imap-open-attachements/#findComment-774929 Share on other sites More sharing options...
Mchl Posted March 2, 2009 Share Posted March 2, 2009 http://www.phpfreaks.com/page/forum-rules Quote Link to comment https://forums.phpfreaks.com/topic/147586-imap-open-attachements/#findComment-774935 Share on other sites More sharing options...
drisate Posted March 2, 2009 Author Share Posted March 2, 2009 i just tryed this $mid = $_GET['num']; $k = $_GET['part']-1; $parttypes = array ("text", "multipart", "message", "application", "audio", "image", "video", "other"); // Load variables only if an external configuration was not selected. function returnAttachment($itemId, $attachId) { global $parttypes, $accountUser, $accountPass, $mbox; $msgStructure = imap_fetchstructure($mbox, $itemId); $part = $msgStructure->parts[$attachId-1]; $ctype = $parttypes[$part->type]."/".$part->subtype; $filename = "filename"; foreach($part->parameters as $param) { if($param->attribute=="NAME") $filename = $param->value; } header("content-type: ".$ctype); header("content-disposition: attachment; filename=".$filename); // Returned data depends on whether the attachment is binary or text if($part->type>0) { // Binary attachment - convert from base64 to binary echo base64_decode(imap_fetchbody($mbox, $itemId, $attachId)); } else { // Text attachment - just display it as-is echo imap_fetchbody($mbox, $itemId, $attachId); } } echo returnAttachment($mid, $k); It's even worse ... i don't get it ... those are codes posted everywhere as working ... This code returns an HTML file type + name of the file is by delfault in the code filename and the file returned is corrupted http://www.phpfreaks.com/page/forum-rules Sorry it's just that when the message is down the page (and has some replys to it) nobody opens it so when it's down i bump it so that somebody hopefully sees this and has the answer i am looking for. Quote Link to comment https://forums.phpfreaks.com/topic/147586-imap-open-attachements/#findComment-774952 Share on other sites More sharing options...
Mchl Posted March 2, 2009 Share Posted March 2, 2009 All fine and well, except that earliest topic visible in PHP Help section is from 18:49. You've bumped your twice after that. So it was visible on first page. Besides, regular forum users browse also beyond first page. Patience. By bumping beyond reason, you're only annoying those who could help you. Quote Link to comment https://forums.phpfreaks.com/topic/147586-imap-open-attachements/#findComment-774960 Share on other sites More sharing options...
Daniel0 Posted March 2, 2009 Share Posted March 2, 2009 Plus you'll look like a fool and people will ignore you. It's counter-productive. Quote Link to comment https://forums.phpfreaks.com/topic/147586-imap-open-attachements/#findComment-775004 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.