PHP线程无法取消链接文件?

时间:2015-04-23 19:06:25

标签: php multithreading

创建新的PHP子线程时,无法使用unlink()删除文件。有这个限制的充分理由还是我忘了什么? 我得到了一个问题:

  

警告:取消关联(下载/ 1e6f6fa1c0552a1af9058f10216b40e8):没有   文件或目录

虽然文件是在目标文件夹中创建的,但是当我在线程函数外部运行相同的命令时,它会删除文件。

//多线程类

<?php    
class download extends Thread {
public $i;
    public $res;
public function __construct($s){
    $this->i = $s;
}
public function run() {
try{
    $url = "http://my.link.com/{$this->i}";
    set_time_limit(0);
    $id = md5(uniqid());
    $tempName = md5($id.time());
    $tmp = "downloads/{$tempName}";
    $fp = fopen (dirname(__FILE__) . '/'.$tmp, 'w+');
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 50);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
    require('scanner.php');
    $results = scanfiles($tmp);
    unlink($tmp);
    $this->res = $results;
}catch(Exception $e){
    $this->res = '0';
}}} ?>

1 个答案:

答案 0 :(得分:2)

进入fopen你传递绝对路径(dirname(__FILE__) . "/{$tmp}")而进入取消链接相对路径时,只需在任何地方使用abslute它应该有效。顺便说一句,自PHP 5.3以来,您只能使用__DIR__代替dirname(__FILE__)