PHP连接FTP服务:
<?php class Ftp { private $connect; private $getback; /** * ftp连接信息 * @var array */ private $ftpConnInfo = [ 'host' => '127.0.0.1', 'port' => '21', 'timeout' => '90', ]; //连接FTP function connect ($user, $passwd) { $this->connect = @ftp_connect(...$this->ftpConnInfo); $login = @ftp_login($this->connect, "{$user}", "{$passwd}"); if ((!$this->connect)(!$login)) { echo "connect failed: {$this->ftpConnInfo['host']} for user {$user} "; die; } else { echo "Connected success "; } } /** * 获取文件最后的修改时间 * @param $remoteFile * @return int the last modified time as a Unix timestamp on success, or -1 on error */ function lastmodtime ($remoteFile) { return ftp_mdtm($this->connect, $remoteFile); } /** * 更改当前目录 * @param $directory * @return bool */ function changedir ($directory) { return ftp_chdir($this->connect, $directory); } /** * 获取当前目录 * @return string */ function getdir () { return ftp_pwd($this->connect); } /** * 获取目录中的文件列表 * @param $directory * @return array */ function getFilelist ($directory) { return ftp_nlist($this->connect, $directory); } /** * 设置被动模式是否开启,true开启 * @param $pasvmode * @return bool */ function setPasvmode ($pasvmode) { //把FTP服务器部署在防火墙或者NAT服务器的背后, //则采用主动操作模式的客户端只能够建立命令连接而无法进行文件传输 return ftp_pasv($this->connect, $pasvmode); } /** * 退出ftp连接 */ function closeFtp () { ftp_quit($this->connect); } }