douzhanlie9209 2015-08-18 10:03
浏览 64
已采纳

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

Question No. 1 - can anybody tell me whats wrong with the code?? I have a script getting only the pdf file....

Question No. 2 - is there a way get the username and password in another php file like this;

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

how can I do thAT?? please prefer to the code below.

<?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条回答 默认 最新

  • dsavz66262 2015-08-18 10:40
    关注

    Look at this line:

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

    Based on the details of the PHP Notice you provided, $part is an instance of stdClass. However, also based on your PHP Notice message it does not have a disposition property.

    Change this line to :

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

    To ensure the property exists before calling it. Alternatively, you can use property_exists(); e.g:

    property_exists($part, 'disposition');
    

    Additionally, you might want to consider adding a condition somewhere to ensure that $part is actually an object with is_object():

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

    Or, a specific instance type with instanceof:

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

    Regarding your second question: if you have two separate questions, you should create post two separate questions. But, if you define any values in a separate .php file and want to include them in your current script, you can simply use require_once to effectively import them; e.g:

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

    Hope this helps :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作