PHP ftp_connect 超时参数不工作
PHP ftp_connect timeout parameter Not working
$conn = ftp_connect($server['host'], $server['port'], 5);
ftp_get(@$conn, 'a.txt', 'a.txt', FTP_ASCII);
以上代码对于大型 a.txt 文件工作了 30 分钟,并且尽管有 5 秒的超时设置,但永远不会结束,根据 ftp_connect 手册 "specifies the timeout in seconds for all subsequent network operations."
请指教
如果你想中止下载,当下载时间太长时,你不能使用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);
}
$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);
}
$conn = ftp_connect($server['host'], $server['port'], 5);
ftp_get(@$conn, 'a.txt', 'a.txt', FTP_ASCII);
以上代码对于大型 a.txt 文件工作了 30 分钟,并且尽管有 5 秒的超时设置,但永远不会结束,根据 ftp_connect 手册 "specifies the timeout in seconds for all subsequent network operations."
请指教
如果你想中止下载,当下载时间太长时,你不能使用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);
}
$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);
}