XFtp连接
private $hostname = ''; \地址
private $username = '';
private $password = '';
private $port = 21;
private $conn_id = FALSE;
/**
* FTP登陆
*
* @access private
* @return boolean
*/
private function _login()
{
return @ftp_login($this->conn_id, $this->username, $this->password);
}
/**
* 关闭FTP
*
* @access public
* @return boolean
*/
public function close()
{
if (!$this->_isconn()) {
return FALSE;
}
return @ftp_close($this->conn_id);
}
/**
* 判断con_id
*
* @access private
* @return boolean
*/
private function _isconn()
{
if (!is_resource($this->conn_id)) {
if ($this->debug === TRUE) {
$this->_error("ftp_no_connection");
}
return FALSE;
}
return TRUE;
}
/**
* 下载
*
* @access public
* @param string 远程目录标识(ftp)
* @param string 本地目录标识
* @param string 下载模式 auto || ascii
* @return boolean
*/
public function download($remotepath, $localpath, $mode = 'auto')
{
if (!$this->_isconn()) {
return FALSE;
}
$mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
$result = @ftp_get($this->conn_id, $localpath, $remotepath, $mode);
if ($result === FALSE) {
if ($this->debug === TRUE) {
$this->_error("ftp_unable_to_download:localpath[" . $localpath . "]-remotepath[" . $remotepath . "]");
}
return FALSE;
}
return TRUE;
}
/**
* 获取目录文件列表
*
* @access public
* @param string 目录标识(ftp)
* @return array
*/
public function filelist($path = '.')
{
if (!$this->_isconn()) {
return FALSE;
}
return ftp_nlist($this->conn_id, $path);
}
以上是ftp一些基础操作,连接,获取文件列表,下载文件,需要获取代码可以私信我获取,
接下来需要进行获取文件操作,并且去除重复的
/**
* 获取远程文件列表
*/
private function getRemoteFileList($path, $config)
{
$this->ftp = new Ftp($config);
$res = $this->ftp->connect($config);
if ($res) {
$fileArr = array();
$list = $this->listFtpDir($path, $fileArr);
$this->ftp->close();
return $list;
}
return false;
}
private function listFtpDir($dir, &$fileArr)
{
$data = $this->ftp->filelist($dir);
if (!is_array($data)) {
return;
}
foreach ($data as $value) {
if ($this->is_json_file($value)) {
$fileArr[] = $value;
} else {
$this->listFtpDir($value, $fileArr);
}
}
return $fileArr;
}
public function file($dataDir)
{
//获取本地文件列表
$localFileList = $this->getBrLocalFileList($dataDir, 'FLIE/');
if (count($localFileList) > 0) {
foreach ($localFileList as $key => $value) {
$localFileList[$key] = str_replace(self::FTP_FILE_DIR, '', $value);
}
}
//获取远程文件列表
$remoteFileList = $this->getBrRemoteFileList($dataDir, 'FLIE');
//获取本地没有的文件列表
$remoteArr = array();
if (count($localFileList) > 0) {
foreach ($remoteFileList as $key => $value) {
if (!in_array($value, $localFileList)) {
$remoteArr[] = $value;
}
}
} else {
$remoteArr = $remoteFileList;
}
//下载文件,对于本地有的不会进行二次下载,上面方法已经过滤
然后对文件需要的操作,
}
/**
* 下载远程文件
*/
private function downRemote2LocalWget($localFtpDir, $list, $config)
{
$this->ftp = new ftp($config);
$this->ftp->connect();
foreach ($list as $value) {
$dir = $localFtpDir . '/' . substr($value, 0, strripos($value, '/'));
if (!is_dir($dir)) mkdir($dir, 0777, true);
//本地存在则删除
if (is_file($localFtpDir . '/' . $value)) {
unlink($localFtpDir . '/' . $value);
}
if (!is_file($localFtpDir . '/' . $value)) {
$filesize = $this->ftp->ftpfilesize($value);
if (!$filesize) {
echo '服务端文件为空!' . "n";
} else {
$re = $this->ftp->download($value, $dir . '/' . substr($value, strripos($value, '/') + 1, strlen($value) - 1));
if ($re) {
echo '下载' . $value . '成功!' . "n";
} else {
echo '下载' . $value . '失败!' . "n";
}
}
}
}
$this->ftp->close();
}
上面是FTP下载文件一些基本操作,只贴了部分代码。如有出入,可以后台联系我,欢迎指正。