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

PHP Notice: Undefined property: stdClass::$disposition

问题 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);


}
}

?>

看看这一行:

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

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

将此行更改为:

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

在调用它之前确保 属性 存在。或者,您可以使用 property_exists();例如:

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

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

<?php

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

希望这对您有所帮助:)