通过PHP内置的FTP函数进行下载多级目录下的所有文件


function getfiles($server,$username,$password,$localpath="D:/ftppath",$serverpath="/"){

//连接ftp服务器
$conn_id = ftp_connect($server);


//登录FTP服务器
$login_result = ftp_login($conn_id, $username, $password);


//开启被动模式
ftp_pasv($conn_id,true);


//返回指定目录的文件列表
$content = ftp_nlist($conn_id, $serverpath);


    //遍历文件列表进行ftp资源的下载
foreach ($content as $key => $value) {
        //文件都存在后缀名
    $result = strrpos($value,".");


    if($result){
       //以二进制的方式下载FTP资源文件
       ftp_get($conn_id,$localpath."/".$value,$value,FTP_BINARY);
    }else{
            //拼凑本地路径
        $local_path = $localpath."/".$value;
        
        //不存在进行创建目录,存在进行递归
        if(!is_dir($local_path)){
            mkdir($local_path,0777,true);
        }else{
            getfiles($server,$username,$password,$localpath,$value);  
        }
    }   
}
}
getfiles($server,$username,$password,$localpath,"/");

你可能感兴趣的:(php)