BenExile / Dropbox

UNMAINTAINED: PHP 5.3 SDK for the Dropbox REST API
MIT License
521 stars 134 forks source link

Sending files from FTP to Dropbox #74

Closed Tklaversma closed 11 years ago

Tklaversma commented 11 years ago

Hi Ben,

First of all, amazing plugin! Thanks for that.. Now my problem..

I would like to send files from FTP to Dropbox. I've already managed to access my files on ftp. The only problem is, how do I 'copy' those files into variables and send them to dropbox. I altered your putFile.php example to the following:

// Require FTP class
require_once '../classes/ftp.class.php';
$ftp = new FTP();

// FTP login
$paths = array();
if ($ftp->login()) {

    // Turn on passive mode so data connections are initiated by the
    // client, rather than by the server. It may be needed if the
    // client is behind firewall. 
    ftp_pasv($ftp->getConnection(), true);

    // Set path to directory
    $FTP_path = "/user_backups";

    // Get contents of the given directory
    $files = ftp_nlist($ftp->getConnection(), $FTP_path);

    // Alphabetic sorting of files in directory
    sort($files);

    // Loop through files and directories
    foreach ($files as $file) {

        // Is file
        if (ftp_size($ftp->getConnection(), $file) != -1) {

            // Get filename
            $name = pathinfo($file, PATHINFO_FILENAME);
            $ext = pathinfo($file, PATHINFO_EXTENSION);
            $filename = str_replace(" ", "%20", $name . '.' . $ext);

            // Set path to backup file and save it in the paths array
            array_push($paths, "ftp://xxx:xxx@xxx{$FTP_path}/{$filename}");
        }
        //else
        // is directory
    }
}

// FTP logout and close
$ftp->logout();

// If backups exist
if (!empty($paths)) {

    // Require the bootstrap (this includes the variable $dropbox)
    require_once 'bootstrap.php';

    // Upload backups to Dropbox
    foreach ($paths as $filepath) {
        try {

            // Access file through $filepath and save it as $file

            $put = $dropbox->putFile($file);
            var_dump($put);

        } catch (\Dropbox\Exception\BadRequestException $e) {
            // The file extension is ignored by Dropbox (e.g. thumbs.db or .ds_store)
            echo "Invalid file extension: {$e}";
        }
        break;
    }
} else {
    echo 'No backups available.';
}

Thanks in advance!

Greets,

Tim

BenExile commented 11 years ago

Hi Tim,

As the file is on a remote server (I'm assuming this as you wouldn't connect via FTP to localhost) you will not be able to upload it to Dropbox unless you download the file to the local machine first, then upload using either the putFile() or chunkedUpload() method.

Let me know of you need further advice.

Tklaversma commented 11 years ago

Hi Ben,

Thanks for quick response.

Ah I see. In that case, I do need your help.

The whole idea is to create cronjob that creates a backup of a website and then pushes it to Dropbox. Since it will be a cronjob - in other word, no local machine - how do I temporarily save files that I download from FTP so I can push them to Dropbox with the current example?

I understand this is a little of topic, but I hope you can help me out.

Greets,

Tim

Op 10 feb. 2013 om 23:36 heeft Ben Tadiar notifications@github.com het volgende geschreven:

Hi Tim,

As the file is on a remote server (I'm assuming this as you wouldn't connect via FTP to localhost) you will not be able to upload it to Dropbox unless you download the file to the local machine first, then upload using either the putFile() or chunkedUpload() method.

Let me know of you need further advice.

— Reply to this email directly or view it on GitHub..

BenExile commented 11 years ago

Hi Tim,

By local machine I mean the box PHP is installed on (the one the cron is running on).

How you download them to this machine first depends on the FTP class you're using - is this something you wrote yourself?

Theoretically you would download all the files you need via FTP, prepare your backup (ZIP or similar) and then upload to Dropbox. I'm unable to provide much more support on this as there is no issue with the library. If you come across any issues I'm more than happy to help.

Tklaversma commented 11 years ago

I understand. You may close this topic.

Thank you.