加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 00:04:58 所属栏目:Linux 来源:网络整理
导读:我正在为在共享主机中运行的客户端编写 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(
我正在为在共享主机中运行的客户端编写 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,$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);)行.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读