Skip to content

Instantly share code, notes, and snippets.

@barooney
Created January 7, 2016 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barooney/3272dde177cbe6188d8c to your computer and use it in GitHub Desktop.
Save barooney/3272dde177cbe6188d8c to your computer and use it in GitHub Desktop.
FTP to Dropbox Migration
{
"name": "barooney/ftp-dropbox-migrator",
"description": "Migrates a full FTP server to a Dropbox",
"type": "console",
"require": {
"league/flysystem": "^1.0",
"league/flysystem-dropbox": "^1.0"
},
"authors": [
{
"name": "Daniel Baron",
"email": "daniel@barooney.com"
}
],
"minimum-stability": "dev"
}
<?php
include './vendor/autoload.php';
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Ftp as FtpAdapter;
use League\Flysystem\Dropbox\DropboxAdapter;
use Dropbox\Client;
define( 'FTP_HOST', 'ftp-server.com' );
define( 'FTP_USER', 'user' );
define( 'FTP_PASSWORD', 'pass' );
define( 'FTP_ROOT', '/' );
define( 'DROPBOX_APP_KEY', 'app-key' );
define( 'DROPBOX_APP_SECRET', 'app-secret' );
define( 'DROPBOX_ACCESS_TOKEN', 'access-token' );
$ftp = new Filesystem( new FtpAdapter( [
'host' => FTP_HOST,
'username' => FTP_USER,
'password' => FTP_PASSWORD,
] ) );
$client = new Client( DROPBOX_ACCESS_TOKEN, DROPBOX_APP_SECRET );
$adapter = new DropboxAdapter( $client );
$dropbox = new Filesystem( $adapter );
echo "Migration started.\n";
$contents = $ftp->listContents( FTP_ROOT, true );
foreach ( $contents as $content ) {
if ( 'dir' === $content['type'] ) {
echo "dir : " . $content['path'] . "\n";
$dropbox->createDir( $content['path'] );
} else if ( 'file' === $content['type'] ) {
echo "file: " . $content['path'] . "\n";
try {
$in = $ftp->readStream( $content['path'] );
$dropbox->writeStream( $content['path'], $in );
if ( is_resource( $in ) ) {
fclose( $in );
}
} catch ( League\Flysystem\FileExistsException $fee ) {
echo "file exists already. skipping.\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment