PHP/Codeigniter FTP 超时

发布于 2024-09-05 18:45:56 字数 288 浏览 5 评论 0原文

我正在尝试使用 Codeigniter 的 FTP 库从我的 PHP 脚本访问 FTP 服务器。这些函数工作得很好,但是在测试脚本时,我发现如果我尝试连接到不存在的服务器,脚本不会终止并显示任何类型的错误消息。

该页面继续执行,直到 Web 服务器放弃并返回一个空文档。

所以我想知道,是否有一种方法可以限制 Codeigniter 尝试连接到 FTP 服务器的时间,然后在超时时显示一条消息?

我尝试使用 php 函数 set_time_limit(),但它的行为不符合我的预期。

感谢您的帮助。

I'm trying to access an FTP server from my PHP script using Codeigniter's FTP Library. These functions work great, but when testing the script I discovered that if I attempt to connect to a server that does not exist, the script does not terminate with an error message of any kind.

The page continues to execute, until the web server gives up, returning an empty document.

So I am wondering, is there a way to limit the amount of time that Codeigniter can try to connect to an FTP server, then display a message if that times out?

I tried using the php function set_time_limit(), but it does not behave how I expected it to.

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南汐寒笙箫 2024-09-12 18:45:56

Codeigniter 的 ftp 类使用底层 ftp_connect php 调用,支持第三个可选参数,超时 (https://www.php.net/manual/en/function.ftp-connect.php)。

然而,Codeigniter 不使用它,但允许扩展它提供的默认库(前提是您愿意做一些工作并检查您对核心所做的任何更新都不会破坏扩展类的功能)。因此,为了解决您的问题,您可以在应用程序库文件夹中创建一个新库:

<?php

class MY_FTP extends CI_FTP { //Assuming that in your config.php file, your subclass prefix is set to 'MY_' like so: $config['subclass_prefix'] = 'MY_';
    
    var $timeout = 90;
    /**
     * FTP Connect
     *
     * @access  public
     * @param   array    the connection values
     * @return  bool
     */
    function connect($config = array())
    {
        if (count($config) > 0)
        {
            $this->initialize($config);
        }
        
        if (FALSE === ($this->conn_id = ftp_connect($this->hostname, $this->port, $this->timeout)))
        {
            if ($this->debug == TRUE)
            {
                $this->_error('ftp_unable_to_connect');
            }
            return FALSE;
        }

        if ( ! $this->_login())
        {
            if ($this->debug == TRUE)
            {
                $this->_error('ftp_unable_to_login');
            }
            return FALSE;
        }

        // Set passive mode if needed
        if ($this->passive == TRUE)
        {
            ftp_pasv($this->conn_id, TRUE);
        }

        return TRUE;
    }
}
?>

并从您的脚本中,您可以将超时选项添加到配置数组中:

$this->load->library('ftp'); //if ftp is not autoloaded
$ftp_params = array('hostname'=>'1.2.3.4', 'port'=>21, 'timeout'=>10); //timout is 10 seconds instead of default 90
$ftp_conn = $this->ftp->connect($ftp_params);
if(FALSE === $ftp_conn) {
//Code to handle error
}

ftp 类不会提供错误消息,除非调试参数设置为在配置数组中为 TRUE,在这种情况下,它只会显示错误。但是它也可以被覆盖,因为所有错误都会调用类中的函数 _error()。所以你可以设置 'debug' => true 在你的 $ftp_params 数组中,并在 MY_ftp 中添加一个函数,如下所示:

/**
 * This function overrides 
 */
function _error($line)
{
    $this->error = $line;
}

然后有一个函数 getError()
/**
* 该函数覆盖
*/
函数 get_error()
{
返回 $this-> 错误;
因此,

如果

$ftp_conn = $this->ftp->connect($ftp_params);

返回 false,您可以调用

$error = $this->ftp->get_error();

以获取错误并显示它。
现在,您始终可以通过进一步自定义类来自定义并拥有更复杂的错误处理机制...

希望它能回答您的问题。

Codeigniter's ftp class uses the underlying ftp_connect php call that supports a 3rd optional parameter, timeout (https://www.php.net/manual/en/function.ftp-connect.php).

Codeigniter however does not use it, but allows for extending the default libraries it provides (providing that you're willing to do some work and check that any updates you do to the core will not break the functionality of your extended class). So to solve your problem you could create a new library in you application library folder:

<?php

class MY_FTP extends CI_FTP { //Assuming that in your config.php file, your subclass prefix is set to 'MY_' like so: $config['subclass_prefix'] = 'MY_';
    
    var $timeout = 90;
    /**
     * FTP Connect
     *
     * @access  public
     * @param   array    the connection values
     * @return  bool
     */
    function connect($config = array())
    {
        if (count($config) > 0)
        {
            $this->initialize($config);
        }
        
        if (FALSE === ($this->conn_id = ftp_connect($this->hostname, $this->port, $this->timeout)))
        {
            if ($this->debug == TRUE)
            {
                $this->_error('ftp_unable_to_connect');
            }
            return FALSE;
        }

        if ( ! $this->_login())
        {
            if ($this->debug == TRUE)
            {
                $this->_error('ftp_unable_to_login');
            }
            return FALSE;
        }

        // Set passive mode if needed
        if ($this->passive == TRUE)
        {
            ftp_pasv($this->conn_id, TRUE);
        }

        return TRUE;
    }
}
?>

and from your script, you could add to your configuration array the timeout option:

$this->load->library('ftp'); //if ftp is not autoloaded
$ftp_params = array('hostname'=>'1.2.3.4', 'port'=>21, 'timeout'=>10); //timout is 10 seconds instead of default 90
$ftp_conn = $this->ftp->connect($ftp_params);
if(FALSE === $ftp_conn) {
//Code to handle error
}

The ftp class is not designed to give error messages unless the debug parameter is set to TRUE in te config array, in which case it'll just display an error. However it can also be override, because all errors call the function _error() in the class. So you could set 'debug' => true in your $ftp_params array, and add a function in MY_ftp like so:

/**
 * This function overrides 
 */
function _error($line)
{
    $this->error = $line;
}

And then have a function getError()
/**
* This function overrides
*/
function get_error()
{
return $this->error;
}

So if

$ftp_conn = $this->ftp->connect($ftp_params);

returns false, you can call

$error = $this->ftp->get_error();

to get your error and display it.
Now, you can always customize and have a more complex error handling mechanism by further customizing the class...

Hope it answers your question.

荒岛晴空 2024-09-12 18:45:56

答案很简单,不要尝试连接到不存在的服务器

The answer is simple, don't attempt to connect to a server that doesn't exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文