如何在PHP

时间:2015-04-28 13:30:05

标签: php multithreading

开始我的线程后

$dnld = new download($arr[$i]);
$dnld->start();

我尝试使用

分离此线程
$dnld->detach();

我收到错误

  

调用未定义的方法download :: detach()

虽然我以同样的方式使用join()而没有问题

$dnld->join();

这是我的线程类

class download extends Thread {
    public $url;
    public $sz;
    public $type;
    public $results;

    public function __construct($s){
        $this->url = $s['1'];
        $this->sz = $s['2'];
        $this->type = $s['3'];
    }

    public function run() {
        try{
                set_time_limit(0);                                  // prevent apache server from timing out while downloading large files
                $id = md5($this->url);                          // create a unique ID for each file (prevent over-write)
                $tmp = __DIR__ ."\\downloads\\{$id}";               // storage location
                $fp = fopen ($tmp, 'w+');                       // open file for writing
                $ch = curl_init(str_replace(" ","%20",$this->url));                 // download file
                curl_setopt($ch, CURLOPT_TIMEOUT, 0);           // request timeout -> never
                curl_setopt($ch, CURLOPT_FILE, $fp);            // file to write to
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                curl_exec($ch);                                 // start download
                curl_close($ch);                                // download complete
                fclose($fp);                                    // close file
                if(($this->sz < (1024*1024*128))&&($this->type == 1)){
                    require('virusTotal.php');
                    $res['type'] = 'virusTotal';
                    $res['res'] = scanFiles($tmp,$this->url);       // send file to scanners
                }else{
                    require('scanner.php');
                    $res['type'] = 'localScan';
                    $res['res'] = scanFiles($tmp);      // send file to scanners
                }

                $this->results = array('result'=>'SUCCESS', 'msg'=>$res);
        }catch(Exception $e){   
            $this->results = array('result'=>'FAILED', 'msg'=>$e);          // return error code
        }
    }
}

1 个答案:

答案 0 :(得分:1)

了解更多: http://php.net/manual/en/thread.detach.php

  

警告

     

此方法可能导致未定义的不安全行为。它不应该   通常使用,它的存在是为了完整和高级使用   例。