使用 PHP 通过 FTP 下载文件夹 [英] Downloading a folder through with FTP using PHP

查看:24
本文介绍了使用 PHP 通过 FTP 下载文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 FTP 和 PHP 从远程主机下载文件夹?

How can i donwload a folder from a remote host using FTP And PHP?

我有用户名和密码以及要下载的文件夹...

I have username and password and the folder to donwload...

复制()?

告诉我,谢谢!

推荐答案

<?php 
$ftp_server = "ftp.example.com"; 
$conn_id = ftp_connect ($ftp_server) 
    or die("Couldn't connect to $ftp_server"); 

$login_result = ftp_login($conn_id, "user", "pass"); 
if ((!$conn_id) || (!$login_result)) 
    die("FTP Connection Failed"); 

ftp_sync ("DirectoryToCopy");    // Use "." if you are in the current directory 

ftp_close($conn_id);  

// ftp_sync - Copy directory and file structure 
function ftp_sync ($dir) { 

    global $conn_id; 

    if ($dir != ".") { 
        if (ftp_chdir($conn_id, $dir) == false) { 
            echo ("Change Dir Failed: $dir<BR>
"); 
            return; 
        } 
        if (!(is_dir($dir))) 
            mkdir($dir); 
        chdir ($dir); 
    } 

    $contents = ftp_nlist($conn_id, "."); 
    foreach ($contents as $file) { 

        if ($file == '.' || $file == '..') 
            continue; 

        if (@ftp_chdir($conn_id, $file)) { 
            ftp_chdir ($conn_id, ".."); 
            ftp_sync ($file); 
        } 
        else 
            ftp_get($conn_id, $file, $file, FTP_BINARY); 
    } 

    ftp_chdir ($conn_id, ".."); 
    chdir (".."); 

} 
?>

来源:http://www.php.net/手册/es/function.ftp-get.php#90910

这篇关于使用 PHP 通过 FTP 下载文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆