PHP注意:未定义的属性:stdClass :: $ disposition

时间:2015-08-18 10:03:31

标签: php imap

问题1 - 任何人都可以告诉我代码有什么问题吗?我有一个脚本只获取pdf文件....

问题2 - 有没有办法在另一个像这样的php文件中获取用户名和密码;

define("Email", "xxxxx.com");
define("Password", "******");

我该怎么办?请更喜欢以下代码。

<?php
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'xxxxx@gmail.com';
$password = '*******';
$savedirpath = "/Attachment";
$type = 'ReadAttachment';
$obj = new $type;
$obj->getdata($hostname ,$username ,$password,$savedirpath);
class ReadAttachment
{
    function getdecodevalue($message,$coding) {
        switch($coding) {
            case 0:
            case 1:
                $message = imap_8bit($message);
                break;
            case 2:
                $message = imap_binary($message);
                break;
            case 3:
            case 5:
                $message=imap_base64($message);
                break;
            case 4:
                $message = imap_qprint($message);
                break;
        }
        return $message;
    }


    function getdata($hostname ,$username ,$password,$savedirpath) {

        $savedirpath = str_replace('\\', '/', $savedirpath);
        if (substr($savedirpath, strlen($savedirpath) - 1) != '/') {
            $savedirpath .= '/';
        }
        $mbox = imap_open ($hostname , $username , $password) or die("can't connect: " . imap_last_error());
        $message = array();
        $message["attachment"]["type"][0] = "text";
        $message["attachment"]["type"][1] = "multipart";
        $message["attachment"]["type"][2] = "message";
        $message["attachment"]["type"][3] = "application";
        $message["attachment"]["type"][4] = "audio";
        $message["attachment"]["type"][5] = "image";
        $message["attachment"]["type"][6] = "video";
        $message["attachment"]["type"][7] = "other";

                $MC = imap_check($mbox);
                $result = imap_fetch_overview($mbox,"1:{$MC->Nmsgs}",0);
                $jk = 1;
                foreach ($result as $overview) {

                    $subj = $overview->subject;
                    $date= strtotime($overview->date);
                    $unixDate = date("YmdHis", $date);
                    $email= $overview->from;
                        $opening = strpos($email, '<')+1;         //   removed username of a sender
                        $closing = strpos($email, '>');           //   removed username of a sender
                    $from = substr($email, $opening, $closing-$opening);




                        $structure = imap_fetchstructure($mbox, $jk);                                 //, FT_UID - for pop3   
                        $parts = (isset($structure->parts) ? $structure->parts : false);
                        $fpos=2;

                        for($i = 1; $i < count($parts); $i++) {

                            $message["pid"][$i] = ($i);
                            $part = $parts[$i];

                            if($part->disposition == "ATTACHMENT") {    

                                $message["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
                                $message["subtype"][$i] = strtolower($part->subtype);
                                $ext=$part->subtype;
                                $params = $part->dparameters;
                                $filename=$part->dparameters[0]->value;
                                $mege="";
                                $data="";
                                $mege = imap_fetchbody($mbox,$jk,$fpos);  
                                $filename="$filename";
                                $extension=pathinfo($filename);

                                $file_extension = $extension['extension'];
                                if($file_extension === "pdf")
                                {
                                    $new_filename = $subject . "*" . $from . "*" . "$unixDate" . ".pdf";
                                    $fp=fopen($savedirpath.$new_filename,'w');
                                    $data=$this->getdecodevalue($mege,$part->type); 
                                    fputs($fp,$data);
                                    fclose($fp);
                                    $fpos+=1;                                   
                                }
                            }                   
                        } imap_delete($mbox,$jk);
                    $jk++;              

                }       

            imap_expunge($mbox);
            imap_errors();
            imap_close($mbox);


}
}

?>

1 个答案:

答案 0 :(得分:0)

看看这一行:

if ($part->disposition == "ATTACHMENT") {

根据您提供的PHP通知的详细信息,$partstdClass的实例。但是,同样基于您的PHP Notice消息,它没有disposition属性。

将此行更改为:

if (isset($part->disposition) && $part->disposition == "ATTACHMENT") {

在调用之前确保属性存在。或者,您可以使用property_exists(); e.g:

property_exists($part, 'disposition');

此外,您可能需要考虑在某处添加条件,以确保$part实际上是is_object()的对象:

is_object($part); // `true` if `$part` is any object

或者,具有instanceof的特定实例类型:

$part instanceof stdClass; // `true` if `$part` is a `stdClass` instance

关于你的第二个问题:如果你有两个单独的问题,你应该创建两个单独的问题。但是,如果您在单独的.php文件中定义任何值并希望将它们包含在当前脚本中,则可以使用require_once来有效地导入它们; e.g:

<?php

require_once __DIR__ . '/constants.php';
echo FOO;

希望这会有所帮助:)