0

after starting my thread

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

I tried to detach this thread using

$dnld->detach();

I get an error

Call to undefined method download::detach()

although I used join() in the same way without problems

$dnld->join();

this is my thread class

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
        }
    }
}
Muhmmad Aziz
  • 393
  • 5
  • 17

1 Answers1

1

Read more: http://php.net/manual/en/thread.detach.php

Warning

This method can cause undefined, unsafe behaviour. It should not usually be used, it is present for completeness and advanced use cases.

Kristiyan
  • 1,655
  • 14
  • 17
  • is there another method to allow the main process to exit without waiting for threads to complete? – Muhmmad Aziz Apr 28 '15 at 14:55
  • http://stackoverflow.com/questions/2585656/threads-in-php And http://stackoverflow.com/questions/858883/run-php-task-asynchronously – Kristiyan Apr 28 '15 at 18:45