简体   繁体   English

无法打开流:打开的文件太多

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

I'm writing a PHP CLI script for a client that runs in a shared hosting. 我正在为在共享主机中运行的客户端编写PHP CLI脚本。 It logs to a file using a simple function like: 它使用简单的函数登录到文件,如:

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);
}

And I get this error: 我收到这个错误:

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

I thought there was an issue with using the same handle, so I changed it to: 我认为使用相同的句柄存在问题,因此我将其更改为:

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);
}

But that didn't work. 但那没用。 I get the error always in the same log line. 我总是在同一个日志行中得到错误。 When I do ulimit -n I get 1024, but that shouldn't be an issue because I'm never open more than one file. 当我做ulimit -n我得到1024,但这不应该是一个问题,因为我从来没有打开过多个文件。 Ideas? 想法?

Spotted the issue. 发现了这个问题。 I'm answering this just in case anyone Googles for the same reason, but I know this answer wasn't implied in the question. 我正在回答这个问题,万一有人因为同样的原因而谷歌,但我知道答案并没有隐含在这个问题中。

I'm using BigCommerce API client and turns out that they were opening a handle per request and making my script crash. 我正在使用BigCommerce API客户端,结果他们正在为每个请求打开一个句柄并使我的脚本崩溃。 Here is how I fixed it: 以下是我修复它的方法:

BigCommerce/API/Connection.php:354-365: 的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();
}

(Added the fclose($handle); ) line. (添加了fclose($handle); )行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM