首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用ftp_get和ftp_nlist通过FTP获取多个文件

使用ftp_get和ftp_nlist通过FTP获取多个文件
EN

Stack Overflow用户
提问于 2017-04-24 16:48:18
回答 1查看 1.6K关注 0票数 1

我正在尝试使用ftp_get和ftp_nlist从另一个域获取多个文件。ftp_nlist需要一个资源和一个字符串,但是下面的内容会返回

ftp_nlist()期望参数1是资源,在

为foreach()提供的无效参数

代码语言:javascript
运行
复制
    <?php
// Connect and login to FTP server
$ftp_server = "hostname";
$ftp_username ="username";
$ftp_userpass = "password";
$includes = "/directory/";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);

// Get file list
    $contents = ftp_nlist($conn_id, $includes);

// Loop through for file 1
foreach ($contents as $file) {
   $local_file = '/path/to/file.php';
   $server_file = '/path/to/file.php';
   ftp_get($conn_id, $local_file, $server_file, FTP_BINARY);
}

// Loop through for file 2
foreach ($contents as $file) {
   $local_file = '/path/to/file.php';
   $server_file = '/path/to/file.php';
   ftp_get($conn_id, $local_file, $server_file, FTP_BINARY);
}

// close connection
ftp_close($ftp_conn);
?>
EN

回答 1

Stack Overflow用户

发布于 2017-04-24 16:54:01

未定义传递给$conn_id的变量ftp_nlist()。您需要将$ftp_conn传递给中的所有 ftp_*函数。(在您的案例中是ftp_get())

检查ftp_close(),以确保您没有忘记关闭连接。

我建议为ftp_函数(如https://github.com/dg/ftp-php )使用一个包装器,以使调试更容易。您将能够使用Exceptions并像这样捕获它们:

代码语言:javascript
运行
复制
try {
    $ftp = new Ftp;
    $ftp->connect($ftp_server);
    $ftp->login($ftp_username, $ftp_userpass);
    $ftp->nlist($includes);

} catch (FtpException $e) {
    echo 'Error: ', $e->getMessage();
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43593715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档