php UPLOAD具有已弃用的构造函数

pgky5nke  于 2023-02-11  发布在  PHP
关注(0)|答案(1)|浏览(146)

我有一个发送短信警报的脚本,但是它在“类上传”上失败了。PHP错误日志显示“与类同名的方法在PHP的未来版本中将不会成为构造函数;UPLOAD有一个已弃用的构造函数”“。有人知道哪里出错了吗?

class UPLOAD
{
    var $URI='';
    var $Host='localhost';
    var $Port='80';
    var $Fields=array();
    var $Files=array();
    var $Headers=array();
    var $Request='';
    var $RequestHeaders='';
    var $RequestBody='';
    var $Response='';
    var $ResponseHeaders='';
    var $ResponseBody='';
    var $log='';
    var $errors=array();
    var $UseProxy=false;
    var $ProxyHost='localhost';
    var $ProxyPort=3128;
    
    // constructor
    function UPLOAD($URI)
    {
        $this->log .= 'Creating object with URI: '.htmlspecialchars($URI)."\n";
        if (preg_match("/^https:\/\/([^\/:]+)[:]?(\d*)?(.*)/i", $URI, $m))
        {
            $this->URI = $URI;
            $this->Host = $m[1];
            $this->Port = ((int)$m[2])==0 ? 80 : (int)$m[2];
            $this->log .= 'Object with URI '.htmlspecialchars($URI)." created.\n";
            $this->log .= '  host: '.htmlspecialchars($this->Host)."\n";
            $this->log .= '  port: '.htmlspecialchars($this->Port)."\n";
        }
        else
        {
            $this->log .= 'Object with URI '.htmlspecialchars($URI)." create failed.\n";
            $this->errors[] = 'Access URI '.htmlspecialchars('"'.$URI.'"').' is not valid';
        }
    }
    // adding field(s) to form
    function SetFields($field, $value='')
    {
        if (is_array($field))
        {
            foreach($field as $k=>$v)
            {
                $this->SetFields($k, $v);
            }
        }
        else
        {
            $this->Fields[] = array($field, $value);
            $this->log .= 'Added field '.htmlspecialchars('"'.$field.'"').' with value '.htmlspecialchars('"'.$value.'"')."\n";
        }
    }
    // adding files to form
    function SetFiles($field, $filename='', $content='')
    {
        if (is_array($field))
        {
            foreach($field as $k=>$v)
            {
                $this->SetFiles($k, $v);
            }
        }
        else
        {
            if (is_array($filename))
            {
                foreach($filename as $k=>$v)
                {
                    $this->SetFiles($field, $k, $v);
                }
            }
            else
            {
                $this->Files[] = array($field, $filename, $content);
                $this->log .= 'Added field '.htmlspecialchars('"'.$field.'"').' of type file with name '.htmlspecialchars('"'.$filename.'"')."\n";
            }
        }
    }
    // send form
    function Send()
    {
        if (!$hosts=gethostbynamel($this->Host))
        {
            $this->errors[] = 'Send failed. Host '.htmlspecialchars('"'.$this->Host.'"').' not found.';
            return false;
        }
        $fp = @fsockopen($this->Host, $this->Port, $errno, $errstr, 3);
        if (!$fp) 
        {
            $this->errors[] = "Connection failed with: ".$errno.' '.htmlspecialchars($errstr);
            return false;
        }
        $this->calculate();

        $out = $this->Request;
        fwrite($fp, $out, strlen($out));
        $result = '';
        while (!feof($fp)) {
            $result .= fgets($fp, 1024);
        }
        $this->Response = $result;
        $i = strpos($result, "\r\n\r\n");
        if ($i>0)
        {
            $this->ResponseHeader = substr($result, 0, $i);
            $this->ResponseBody   = substr($result, $i+4);
        }
        else
        {
            $this->ResponseHeader = $result;
            $this->ResponseBody   = '';
        }
        return true;
    }
    // calculate form
    function calculate()
    {
        $boundary = '---------------------------PHPUPLOADCLASS';
        $body = '';
        foreach($this->Fields as $k=>$v)
        {
            $body .= "--$boundary\r\n";
            $body .= 'Content-Disposition: form-data; name="'.$v[0]."\"\r\n";
            $body .= "\r\n".$v[1]."\r\n";
        }
        foreach($this->Files as $k=>$v)
        {
            $body .= "--$boundary\r\n";
            $body .= 'Content-Disposition: form-data; name="'.$v[0].'"; filename="'.$v[1]."\"\r\n";
            $body .= "Content-Type: application/octet-stream\r\n";
            $body .= "\r\n".$v[2]."\r\n";
        }
        $body .= "--$boundary--\r\n";

        $headers = 'POST '.$this->URI." HTTPS/1.0\r\n";
        $headers .= 'Host: '.$this->Host."\r\n";
        $headers .= "User-Agent: Mozilla/4.0 (PHP Uploader Class)\r\n";
        $headers .= "Accept: */*\r\n";
        $headers .= "Pragma: no-cache\r\n";
        foreach($this->Headers as $k=>$v)
        {
            $headers .= $k.': '.$v."\r\n";
        }
        $headers .= "Content-Type: multipart/form-data; boundary=$boundary\r\n";
        $headers .= "Content-Length: ".strlen($body)."\r\n";
        $headers .= "Connection: Close\r\n\r\n";

        $this->Request = $headers.$body;
        $this->RequestHeaders = $headers;
        $this->RequestBody = $body;
    }
}

[编码]

vc6uscn9

vc6uscn91#

在PHP 7中,旧样式的构造函数与类名同名已被弃用。
因此,您需要使用与类名不同的其他方法名。
或者您可以使用PHP中的一个魔术方法,它通常以2个下划线开头,而不是function UPLOAD

    • 喜欢**
public function __construct() 
 {
    //put all your logic to instantiate an object with class properties           
 }
    • 代替:**
public function UPLOAD() 
 {
    //put all your logic to instantiate an object with class properties           
 }

相关问题