簡體   English   中英

無法打開流:打開的文件太多

[英]Failed to open stream: Too many open files

我正在為在共享主機中運行的客戶端編寫PHP CLI腳本。 它使用簡單的函數登錄到文件,如:

function log_entry($msg) {
    global $log_file, $log_handle;
    $msg =  "[".date('Y-m-d H:i:s')."] ".$msg."\n";
    echo $msg;
    $log_handle = fopen($log_file, 'a');
    fwrite($log_handle, $msg);
}

我收到這個錯誤:

PHP Warning:  fopen(./logs/sync.20130410.log) 
[<a href='function.fopen'>function.fopen</a>]: failed to open stream: 
Too many open files in ./functions.php on line 61

我認為使用相同的句柄存在問題,因此我將其更改為:

function log_entry($msg) {
    global $log_file;
    $msg =  "[".date('Y-m-d H:i:s')."] ".$msg."\n";
    echo $msg;
    $log_handle = fopen($log_file, 'a');
    fwrite($log_handle, $msg);
    fclose($log_handle);
}

但那沒用。 我總是在同一個日志行中得到錯誤。 當我做ulimit -n我得到1024,但這不應該是一個問題,因為我從來沒有打開過多個文件。 想法?

發現了這個問題。 我正在回答這個問題,萬一有人因為同樣的原因而谷歌,但我知道答案並沒有隱含在這個問題中。

我正在使用BigCommerce API客戶端,結果他們正在為每個請求打開一個句柄並使我的腳本崩潰。 以下是我修復它的方法:

的Bigcommerce / API / Connection.php:354-365:

public function put($url, $body)
{
    $this->addHeader('Content-Type', $this->getContentType());

    if (!is_string($body)) {
        $body = json_encode($body);
    }

    $this->initializeRequest();
    $handle = tmpfile();
    fwrite($handle, $body);
    fseek($handle, 0);
    curl_setopt($this->curl, CURLOPT_INFILE, $handle);
    curl_setopt($this->curl, CURLOPT_INFILESIZE, strlen($body));

    curl_setopt($this->curl, CURLOPT_URL, $url);
    curl_setopt($this->curl, CURLOPT_PUT, true);
    curl_exec($this->curl);
    fclose($handle); // Added this line

    return $this->handleResponse();
}

(添加了fclose($handle); )行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM