PHP ftp_cdup() 函数用于将当前目录更改为 FTP 服务器上的父目录。
语法
ftp_cdup(ftp)
参数
ftp | 必需。 指定要使用的 FTP 连接。 |
返回值
成功时返回 true,失败时返回 false。
示例:
下面的示例显示了ftp_cdup()函数的用法。
<?php
//要使用的FTP服务器
$ftp_server = "ftp.example.com";
//FTP 连接的用户名
$ftp_user = "user";
//用户密码
$ftp_pass = "password";
//建立连接或者连接失败
$ftp = ftp_connect($ftp_server)
or die("Could not connect to $ftp_server");
if($ftp) {
echo "Successfully connected to $ftp_server!\n";
//尝试登录
if(@ftp_login($ftp, $ftp_user, $ftp_pass)) {
echo "Connected as $ftp_user@$ftp_server\n";
//将当前目录更改为public_html
ftp_chdir($ftp, "public_html");
//显示当前目录
echo "Current directory: ".ftp_pwd($ftp)."\n";
//返回上级目录
if (ftp_cdup($ftp)) {
echo "cdup successful\n";
} else {
echo "cdup not successful\n";
}
//显示当前目录
echo "Current directory: ".ftp_pwd($ftp)."\n";
} else {
echo "Couldn't connect as $ftp_user\n";
}
//关闭连接
if(ftp_close($ftp)) {
echo "Connection closed successfully!\n";
}
}
?>
上述代码的输出将是:
Successfully connected to ftp.example.com!
Connected as user@ftp.example.com
Current directory: /public_html
cdup successful
Current directory: /
Connection closed successfully!