需要在 php 函数上设置超时,如 fopen、file_get_contents 和 fread



因为套接字/流等待时间不包括在PHP最大执行时间中。 为了提高服务器性能, 我需要为 fopen、file_get_contents 和 fread 函数设置超时 如果它们不在 PHP 执行时间内处理。 我尝试过以下示例程序:

$fileName = 'C:/svn/trunk/storage/invoices/BAI_Export.txt';
$fp       = fopen( $fileName, 'rb' );
if( !$fp ) {
echo "Unable to openn";
}else {
stream_set_timeout( $fp, 2 );
$res = fread( $fp, filesize( $fileName ) );
$info = stream_get_meta_data( $fp );
fclose( $fp );
if( $info['timed_out'] ) {
echo 'Connection timed out!';
}else {
var_dump( $info );
}
}

**输出:**

Array
(
[timed_out] => 
[blocked] => 1
[eof] => 
[wrapper_type] => plainfile
[stream_type] => STDIO
[mode] => rb
[unread_bytes] => 0
[seekable] => 1
[uri] => C:/svn/trunk/storage/invoices/BAI_Export.txt
)

在输出中,"time_out"参数为空。 如果有替代解决方案,请告诉我。

也许你可以以下

set_time_limit(0); # If set to zero, no time limit is imposed. 

最新更新