【发布时间】:2020-10-26 11:48:50
【问题描述】:
我目前正在开发一个PHP library,它允许按照RFC959 标准在低级别实现 FTP 功能,因此您可以使用它来创建特别适合您需要的 FTP 功能,或者您可以开发一个整个 FTP 客户端库,无需 FTP 扩展。
几个小时以来,我一直在努力解决一个问题,我有一个方法FtpCommandStream::receive(),它可以用来在使用FtpCommandStream::send() 方法发送一个FTP命令后接收FTP回复字符串表示,这个问题如果我调用receive 方法而不发送任何命令或者控制流为空,则会触发无限循环。
我不会在内部不发送任何命令的情况下调用此方法,库用户可能会调用receive 方法两次或在发送任何命令之前,这将触发无限循环并导致其他副作用。
receive()方法代码:
/**
* @inheritDoc
*/
public function receive()
{
$response = '';
// Check first if the command stream is empty or not!!
while (true) {
$line = fgets($this->stream);
$response .= $line;
/**
* To distinguish the end of an FTP reply, the RFC959 indicates that the last line of
* a the reply must be on a special format, it must be begin with 3 digits followed
* by a space.
*
* @link https://www.rfc-editor.org/rfc/rfc959#section-4
*/
if (preg_match('/\d{3}+ /', $line) !== 0) {
break;
}
}
$this->log($response);
return $response;
}
为了解决这个问题,我们必须在尝试使用fgets 或fread 函数读取命令流之前检查命令流是否为空,我尝试使用foef 但不起作用(有效只有数据流见here),我真的很难找到解决方案,所以任何帮助将不胜感激。提前致谢!
【问题讨论】: