How to copy files from server to Dropbox using PHP? [duplicate]

妖精的绣舞 提交于 2019-11-30 01:01:08
Alireza Noori

What @halfer is suggesting is this (I just modified your potential solution based on his idea) so he should take credit:

<?php

function uploadx($dirtocopy, $dropboxdir, $uploader){
    if ($handle = opendir($dirtocopy)) {
        while (false !== ($entry = readdir($handle))) {
            if ($entry != "." && $entry != "..") {

                if(is_dir($entry)){
                    uploadx($dirtocopy.$entry.'/', $dropboxdir.$entry.'/', $uploader);
                } else {
                    $uploader->upload($dirtocopy.$entry, $dropboxdir.$entry);
                }

            }
        }
        closedir($handle);
    }
}

// Dropbox username/password
$dropbox_email='dropbox@dropbox.com';
$dropbox_pass='password';

// File to backup
$siteroot = "./";

include("DropboxUploader.php");

$uploader = new DropboxUploader($dropbox_email, $dropbox_pass);

uploadx($siteroot, 'Backup/Files/', $uploader);

?>

BTW, the function above is from here: How to backup files from a specific directory to Dropbox using PHP only?

Here's a snippet of code from the PHP site that I pointed you to in the comments. All it does is take a directory path (as a string) and output the full pathname of all files inside it (as a number of strings). This is very useful, as we can use this ability to do something to files on an individual basis (like upload them to Dropbox).

<?php

$path = realpath('/etc');
$objects = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($path),
    RecursiveIteratorIterator::SELF_FIRST
);
foreach($objects as $name => $object) {
    echo "$name\n";
}

So, since you're wanting to learn PHP, and since you'll not learn anything if I give you a ready-made solution :-), try the following:

  1. Remove the system call from your code, since you don't want to do any compression
  2. Get the snippet I've provided working on your machine (it will work on its own), changing '/etc' to whatever path you want
  3. Change $path in my code to $siteroot which you use in yours
  4. Remove the $path = ... line in my code (since you define $siteroot yourself)
  5. Test the snippet again
  6. Add in the $objects line from my code after you define $siteroot, into your code
  7. Wrap the $uploader->upload() line in your code with the for loop I provide, removing the echo statement from my code
  8. Change $name to $backup_files in your code

Jiggle it all about a bit, and it should work. Good luck, and feel free to ask questions!

What about using the command line version of dropbox? http://www.dropboxwiki.com/Using_Dropbox_CLI

Copy the files to the dropbox folder and let the deamon do what it needs to do.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!