PHP - IMAP:将附件保存到特定文件夹

时间:2017-06-26 15:47:20

标签: php

我发现这段代码效果不错,但我需要它:

1。 (必须)将附件保存到服务器上的特定文件夹,而不是.php文件所在的位置。

2。 (如果可能)现在代码生成一些零字节大小的文件。 (我可以编写一些检查文件大小的内容,然后删除大小为零的文件,但如果可能的话,我想首先禁止它被创建。

 <?php

    function getFileExtension($fileName){
       $parts=explode(".",$fileName);
       return $parts[count($parts)-1];
    }

    $imap = imap_open($server, $username, $password) or die("imap connection error");
    $message_count = imap_num_msg($imap);
    for ($m = 1; $m <= $message_count; ++$m){

        $header = imap_header($imap, $m);
        //print_r($header);

        $email[$m]['from'] = $header->from[0]->mailbox.'@'.$header->from[0]->host;
        $email[$m]['fromaddress'] = $header->from[0]->personal;
        $email[$m]['to'] = $header->to[0]->mailbox;
        $email[$m]['subject'] = $header->subject;
        $email[$m]['message_id'] = $header->message_id;
        $email[$m]['date'] = $header->udate;

        $from = $email[$m]['fromaddress'];
        $from_email = $email[$m]['from'];
        $to = $email[$m]['to'];
        $subject = $email[$m]['subject'];

        echo $from_email . '</br>';
        echo $to . '</br>';
        echo $subject . '</br>';

        $structure = imap_fetchstructure($imap, $m);

        $attachments = array();
        if(isset($structure->parts) && count($structure->parts)) {

            for($i = 0; $i < count($structure->parts); $i++) {

                $attachments[$i] = array(
                    'is_attachment' => false,
                    'filename' => '',
                    'name' => '',
                    'attachment' => ''
                );

                if($structure->parts[$i]->ifdparameters) {
                    foreach($structure->parts[$i]->dparameters as $object) {
                        if(strtolower($object->attribute) == 'filename') {
                            $attachments[$i]['is_attachment'] = true;
                            $attachments[$i]['filename'] = $object->value;
                        }
                    }
                }

                if($structure->parts[$i]->ifparameters) {
                    foreach($structure->parts[$i]->parameters as $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($imap, $m, $i+1);
                    if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
                        $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                    }
                    elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
                        $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                    }
                }
            }
        }

        foreach ($attachments as $key => $attachment) {
            $name = $attachment['name'];
            $contents = $attachment['attachment'];
            file_put_contents($name, $contents);
        }

        //imap_setflag_full($imap, $i, "\\Seen");
        //imap_mail_move($imap, $i, 'Trash');
    }

    imap_close($imap);

    ?>

3 个答案:

答案 0 :(得分:2)

您可以将文件写入您喜欢的任何文件夹,rename()功能可用于此目的

http://php.net/manual/en/function.rename.php

rename('image1.jpg', 'del/image1.jpg');

如果您想将现有文件保留在同一位置,则应使用复制

http://php.net/manual/en/function.copy.php

copy('image1.jpg', 'del/image1.jpg');

您可以在put_file_contents()之后使用代码在目录/文件夹

周围移动文件

编辑:在另一个目录中创建文件,而不是在创建后移动它

file_put_contents('./myDir/myFile', $file);

.表示项目的当前目录。

此外,请记住file_put_contents()不会创建他们需要存在的文件夹/目录,或者您需要使用mkdir()

创建它们

答案 1 :(得分:1)

替换

download_file = self.bucket.download_file(kch_file_to_be_downloaded_key, destination_of_kch_file_to_be_downloaded)

使用

foreach ($attachments as $key => $attachment) {
            $name = $attachment['name'];
            $contents = $attachment['attachment'];
            file_put_contents($name, $contents);
}

它会将您的附件保存到您的根目录,您可以从那里随意移动它。

答案 2 :(得分:0)

您可以将文件直接放在所需的路径上,如下所示:

$mypath = '../../myfiles/';
foreach ($attachments as $attachment) {
           file_put_contents( $mypath . $attachment['name'], $attachment['attachment']);
 }
相关问题