Linux 无法打开流:打开的文件太多
我正在为在共享主机中运行的客户端编写一个PHP CLI脚本。它使用以下简单函数登录到文件:Linux 无法打开流:打开的文件太多,linux,logging,php,Linux,Logging,Php,我正在为在共享主机中运行的客户端编写一个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); }
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
但那没用。我得到的错误总是在同一个日志行。当我执行
ulimit-n
时,我得到1024个,但这不应该是一个问题,因为我从未打开过多个文件。想法?发现了这个问题。我回答这个问题只是为了以防万一有人因为同样的原因在谷歌上搜索,但我知道这个答案并没有隐含在问题中
我使用的是BigCommerceAPI客户机,结果发现他们为每个请求打开了一个句柄,使我的脚本崩溃。下面是我如何修复它的:
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);
)行。您的共享主机提供程序可能会遇到文件描述符限制,但我不是一次只使用一个,同时使用两个函数吗?第一个应该在整个执行过程中只使用一个,第二个应该每行使用一个。。。还是工作方式不同。。。apache可以配置为将其ulimit降低到已打开的文件数,从而限制您打开其他文件。您应该与您的主机提供商在config@Zak这是一个PHP脚本,但是它是通过CLI执行的,所以Apache不在中间。
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();
}