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?
Asked
Active
Viewed 1,425 times
1 Answers
0
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.

marcell
- 1,498
- 1
- 10
- 22