我正在尝试使用ftp_get和ftp_nlist从另一个域获取多个文件。ftp_nlist需要一个资源和一个字符串,但是下面的内容会返回
ftp_nlist()期望参数1是资源,在
和
为foreach()提供的无效参数
<?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);
?>
发布于 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
并像这样捕获它们:
try {
$ftp = new Ftp;
$ftp->connect($ftp_server);
$ftp->login($ftp_username, $ftp_userpass);
$ftp->nlist($includes);
} catch (FtpException $e) {
echo 'Error: ', $e->getMessage();
}
https://stackoverflow.com/questions/43593715
复制相似问题