简体   繁体   English

PHP FTP - 在特定目录而不是根目录中下载文件

[英]PHP FTP - Download files in certain directory instead of root

I have a php script at /Users/John/Site/Code/pdfsync.php我在 /Users/John/Site/Code/pdfsync.php 有一个 php 脚本

In the script I have, I am connecting to an FTP, and downloading the files recursively, however it is creating folders and download files at /Users/John/Site/, but I can't seem to figure out how to have the files downloaded at a specific location.在我的脚本中,我正在连接到 FTP,并递归下载文件,但是它正在 /Users/John/Site/ 创建文件夹和下载文件,但我似乎无法弄清楚如何获取这些文件在特定位置下载。 Let's say I when I run the script, I want it to create a PDF folder, so all the files are downloaded at /Users/John/Site/Code/PDF/, not sure how to achieve this.假设我在运行脚本时希望它创建一个 PDF 文件夹,因此所有文件都下载到 /Users/John/Site/Code/PDF/,不知道如何实现。

$ftp_server = 'test.fr';
$ftp_user_name = 'test';
$ftp_user_pass = 'test';
$server = 'test.fr';

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

if ((!$conn_id) || (!$login_result))
    die("FTP Connection Failed");

ftp_sync (".");

echo "Done";
ftp_close($conn_id);


function ftp_sync ($dir) {
    global $conn_id;

    if ($dir != ".") {
        if (ftp_chdir($conn_id, $dir) == false) {
            echo ("Change Dir Failed: $dir<BR>\r\n");
            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 ("..");
}

You can do this very easily by using this library :你可以使用这个很容易地做到这一点:

The code:代码:

$connection = new FtpConnection('host', 'username', 'password');

$client = new FtpClient($connection);

if (asyncDownload('Users/John/Site/Code/PDF', '.')) {
    echo 'Done!';
}

function syncDownload($localDir, $remoteDir)
{
    global $client;
    
    if (!is_dir($localDir)) {
        mkdir($localDir);
    }
    
    /**
     * listDirectoryDetails method will recursively gets all the files with
     * their details within the giving directory.
     */
    $files = $client->listDirectoryDetails($dir, true);
    
    foreach($files as $file) {
        $client->download($file['path'], $localDir . '/' . $file['name'], true, FtpWrapper::BINARY);
    }
    
    return true;
}

The way your code works, the easiest solution is to change the current local working directory to the target path, before calling ftp_sync :您的代码的工作方式,最简单的解决方案是在调用ftp_sync之前将当前本地工作目录更改为目标路径:

chdir("/Users/John/Site/Code/PDF/");
ftp_sync (".");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM