需要帮助编辑代码来使用php ftp_get复制文件

时间:2018-05-12 09:36:45

标签: php file get ftp copy

我试图将文件夹中的所有文件从外部html文件夹中获取到html文件夹/子目录。 我从github看到了这段代码

<?php
// ftp_sync - copy directory and file structure
// based on http://www.php.net/manual/es/function.ftp-get.php#90910
// main function witch is called recursivly
function ftp_sync($dir, $conn_id) {
    if ($dir !== '.') {
        if (ftp_chdir($conn_id, $dir) === FALSE) {
            echo 'Change dir failed: ' . $dir . PHP_EOL;
            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, $conn_id);
        } else {
            ftp_get($conn_id, $file, $file, FTP_BINARY);
        }
    }
    ftp_chdir($conn_id, '..');
    chdir('..');
}
// your settings
$ftp_server    = 'yourdomin.tld';
$user          = 'user';
$password      = 'password';
$document_root = 'html';
$sync_path     = 'dir2copy';
// start copying
echo '<pre>' . PHP_EOL;
echo 'start copying....' . PHP_EOL;
$conn_id = ftp_connect($ftp_server);
if ($conn_id) {
    $login_result = ftp_login($conn_id, $user, $password);
    if ($login_result) {
        ftp_chdir($conn_id, $document_root);
        ftp_sync($sync_path, $conn_id);
        ftp_close($conn_id);
    } else {
        echo 'login to server failed!' . PHP_EOL;
    }
} else {
    echo 'connection to server failed!';
}
echo 'done.' . PHP_EOL;
?>

使用此代码复制所有ftp root文件夹和文件to html文件夹 需要帮助将目录从ftp root / subfolder更改为html /子文件夹,在代码中更改此目录,寻求帮助,谢谢

0 个答案:

没有答案