dregduc63699 2017-12-23 16:16
浏览 143

PHP脚本将文件从一个ftp传输到另一个ftp [重复]

This question already has an answer here:

I want run a php script on a server where are no files to transfer. This script should transfer files from one ftp (with password) to another ftp (with password). It is possible in php? ftp_fput allows only to transfer a local file to a ftp-server? Is it right?

</div>
  • 写回答

1条回答 默认 最新

  • douzinei6926 2017-12-23 16:36
    关注

    I suggest you to use thephpleague's Flysystem FTP adapter. You can create a script based on this package and use it on your control server in order to transfer files from ftp server A to ftp server B.

    Here is an example script that transfer data from server A to server B using sftp. NOTE: The following script assumes you have installed the flysystem package (e.g. composer require league/flysystem).

    <?php
    
    require_once __DIR__ . '/vendor/autoload.php';
    
    use League\Flysystem\Filesystem;
    use League\Flysystem\Sftp\SftpAdapter;
    
    $source = new Filesystem(new SftpAdapter([
        'host' => 'server A',
        'port' => 'server A port',
        'username' => 'server A sftp user',
        'password' => 'server A sftp pwd',
        'root' => 'source folder on server A',
        'timeout' => 10,
    ]));
    
    $destination = new Filesystem(new SftpAdapter([
        'host' => 'server B',
        'port' => 'server B port',
        'username' => 'server B sftp user',
        'password' => 'server B sftp pwd',
        'root' => 'destination folder on server B',
        'timeout' => 10,
    ]));
    
    $files = $source->listContents();
    
    foreach ($files as $file) {
        $data = $source->read($file['path']);
        $destination->put($file['path'], $data);
    }
    

    It establishes a connection to the source ($source) and the destination ($destination) servers. List the contents on the source (listContents() method) and in a foreach loop it reads the files (read()) and put their content (put()) to the desired place one by one.

    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端