![](/img/trans.png)
[英]curl_multi PHP: How to get resource id's of all active easy handles in a master handle
[英]Get all opened resource handle in PHP
我一直在使用PHP應用程序,它使用方法fopen
和ftp_connect
很多。 運行應用程序的服務器之一會在打開太多文件時拋出Fatal Exception
,因此我需要找到所有使用該方法並重構它們的類,以便之后使用fclose
和ftp_close
關閉文件句柄。
通過這樣做,我想在之后添加一個集成測試,跟蹤所有打開的資源,以便這樣的事情不會再發生。
有沒有辦法在PHP中執行此操作?
您可以使用get_defined_vars()
, gettype()
和 - 可選 - get_resource_type()
獲得所需的結果:
$resources = array();
foreach( get_defined_vars() as $key => $val )
{
if( 'resource' == gettype( $val ) )
{
$resources[ get_resource_type( $val ) ][] = $key;
}
}
foreach( $resources as $type => $res )
{
echo sprintf( '%- 20s: % 3d%s', $type, count($res), PHP_EOL );
}
假設您擁有這些已打開的資源:
$handle = fopen( '/Your/File/Path' );
$ftp1 = ftp_connect ( 'ftp.site1.com' );
$ftp2 = ftp_connect ( 'ftp.site2.com' );
上面的代碼將輸出:
stream : 1
FTP Buffer : 2
PHP 7.0.0引入了一個獲取資源的函數,名為get_resources()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.