【问题标题】:PHP ftp_connect timeout parameter Not workingPHP ftp_connect 超时参数不起作用
【发布时间】:2020-09-21 21:48:15
【问题描述】:
$conn = ftp_connect($server['host'], $server['port'], 5);
ftp_get(@$conn, 'a.txt', 'a.txt', FTP_ASCII);

上面的代码为一个大的 a.txt 文件工作了 30 分钟,尽管超时设置为 5 秒,但永远不会结束,根据 ftp_connect 手册“指定所有后续网络操作的超时时间,以秒为单位。”

请指教

【问题讨论】:

  • 我正在尝试在一段时间后终止从 FTP 服务器下载文件并让脚本继续运行

标签: php ftp timeout


【解决方案1】:

如果你想中止下载,当下载时间过长时,你不能使用ftp_get,因为它被阻塞了。


你有两个选择。要么使用ftp_nb_get:

$ret = ftp_nb_get($conn_id, $local_path, $remote_path, FTP_BINARY);

while ($ret == FTP_MOREDATA)
{
    if (takes_too_long) break;
    $ret = ftp_nb_continue($conn_id);
}

FTP URL protocol wrapper:

$url = "ftp://username:password@ftp.example.com/remote/source/path/file.zip";
$hin = fopen($url, "rb") or die("Cannot open source file");
$hout = fopen("/local/dest/path/file.zip", "wb") or die("Cannot open destination file");

while (!feof($hin))
{
    if (takes_too_long) break;
    $buf = fread($hin, 10240);
    fwrite($hout, $buf);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 2014-12-14
    • 2015-07-02
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多