Jump to content

Path to Downloading Attachment


Joob

Recommended Posts

Hello,

I have a problem with my code.. I cant do download attachments, when I click in attachment "image.jpg", the link appears this way "www.site.com/image.jpg" but the link doesn't existed, and go to automatically to my home page without download the file..

$id = $_POST['id']; 
$pdo = new PDO("sqlite:mydb.db"); 
$email = $pdo->query("SELECT * FROM `myemails` WHERE `id`='$myid'")->fetch(PDO::FETCH_ASSOC); 

$email_body = base64_decode($email['message']); 

$letter_html = ""; 
$op_getKey = preg_match_all("/boundary=\"(.*)\"/", $email_body, $getKey); 
$has_attachment = preg_match_all("/multipart\/mixed/", $email_body, $attachment); 
if ($op_getKey) { 
    if ($has_attachment) { 
        $mailParts = explode("--" . $getKey[1][1], $email_body); 
        $attachParts = explode("--" . $getKey[1][0], $email_body); 

        $get_attach_name=preg_match_all("/filename=\"(.*?)\"/", $attachParts[2], $attach_name); 
        $get_attach_type=preg_match_all("/Content-Type: (.*?);/", $attachParts[2], $attach_type); 
        $pure_code = explode("\n\n",$attachParts[2]); 
        $pure_code = str_replace("\r","",$pure_code[1]); 
        $pure_code = str_replace("\n","",$pure_code); 
    }else{ 
        $mailParts = explode("--" . $getKey[1][0], $email_body); 
    } 

Can you give me some help implementing this code in my code?

$savedir = __DIR__ . '/attachment/';

    $myemail = $email->search('ALL');
    if ($myemail ) {
        rsort($myemail );
        foreach ($myemail as $my2email) {
            foreach ($my2email->getAttachments() as $attachment) {
                $savepath = $savedir . $attachment->getFilename();
                file_put_contents($savepath, $attachment);
            }
        }
    }

So that it is possible, whenever you receive an email, that the file go to the "attachment" folder and can download the image or file ..

When I click in attachment and I just wanted that's possible to download the image, pdf, txt, zip, rar, etc.. once they are saved in the attachment folder.

I already called the attachment of these two forms, but nothing..
 

PHP Code:
<a target="_blank" href="https://site.com/<?=$attach_name[1][0]?>" data-target="<?=$id?>"><?=$attach_name[1][0]?></a> 

<a target="_blank" href="data:<?=$attach_type[1][0]?>;base64,<?=$pure_code?>"> <?=$attach_name[1][0]?> </a>

 

I don't know if u understand my problem.

Best Regards

 

Edited by Joob
Link to comment
Share on other sites

@justsomeguy thank you for ur reply.

The email was supposed to fetch the information from this database table, and save the attachments in the attachment folder.

I'm just having trouble with attachments. When clicking on the attachment, the name of the file that was supposed to be sent, "imagen.jpg or mypdf.pdf", appears until then, but when I go to click on the file, to download, the link appears well, but redirects to the initial page and it is only with the link thus "www.site.com/attachment/imagem.jpg, etc ...", that is, it is not receiving the attachments.

I was trying to adapt this code (to my code)..

$savedir = __DIR__ . '/attachment/';

    $myemail = $email->search('ALL');
    if ($myemail ) {
        rsort($myemail );
        foreach ($myemail as $my2email) {
            foreach ($my2email->getAttachments() as $attachment) {
                $savepath = $savedir . $attachment->getFilename();
                file_put_contents($savepath, $attachment);
            }
        }
    }
Edited by Joob
Link to comment
Share on other sites

I've been looking, and I think it's something more like this ..
How can I implement this code down in my code

//------------------------ ATTACHMENTS ------------------------------------//

//loop through email parts
foreach($decoded[0]['Parts'] as $part){
	
	//check for attachments
	if($part['FileDisposition'] == 'attachment'){
		
		//format file name (change spaces to underscore then remove anything that isn't a letter, number or underscore)
		$filename = preg_replace('/[^0-9,a-z,\.,_]*/i','',str_replace(' ','_', $part['FileName']));
		
		//write the data to the file
		$fp = fopen('save_dir/' . $filename, 'w');
		$written = fwrite($fp,$part['Body']);
		fclose($fp);
		
		//add file to attachments array
		$attachments[] = $part['FileName'];

	}

}

//print out the attachments for debug
print_r($attachments);

 

What u think.. ?

Link to comment
Share on other sites

The email was supposed to fetch the information from this database table, and save the attachments in the attachment folder.

That's not what I'm talking about.

I was trying to adapt this code (to my code)..

That's what I'm talking about.  Inside that piece of code, there's a variable called $email.  That's the one I'm asking about, not the variable with the same name in the other piece of code.  You need to figure out what that $email object is if you want to use that code.

How can I implement this code down in my code

First, stop assuming that you can copy and paste any random piece of code and have it work.  Both of those pieces of code have requirements.  The first one requires an object called $email, and I don't know what that object is supposed to be, I don't know if you do either.  The new one uses a variable called $decoded, so again, what is that variable supposed to be?  These aren't standalone pieces of code, it's like you pulled them out from the middle of some other code and you're assuming that I can tell you where they came from or something.  Neither of them are complex but both of them require other things in the code where they came from.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...