How to backup files from a specific directory to Dropbox using PHP only?

让人想犯罪 __ 提交于 2019-11-30 16:34:34

You need recursive code.

Write a function that takes a dir as its argument.

Have it loop through the dir looking at each file. For each file, it checks if it's a dir, and if it's not, it copies it.

If it is a dir, the function calls itself.

e.g.

// your code
require 'DropboxUploader.php';

$dirtocopy = './example_directory/';
$dropboxdir = 'backupdir/';
$uploader = new DropboxUploader('sample-email@gmail.com', 'password');// enter dropbox credentials

$errors = array(); // to store errors.


// function definition
function copyDirRecursive($dir) {
  global $uploader; // makes the "$uploader" below the one from outside the function
  if(is_dir($dir)) { // added if/else to check if is dir, and create handle for while loop
    $handle = opendir($dir); 
    if($handle === false) { // add if statements like this wherever you want to check for an error
      $errors[] = $php_errormsg; // http://php.net/manual/en/reserved.variables.phperrormsg.php
    }
  } else {
    return false;
  }
  while(false !== ($file = readdir($handle))) { // changed foreach to while loop
    if(!isdir($file)) {
      // copy the file
      // cp $dir . '/' . $file to $dropbox . '/' . $dir . '/' . $file; // pseudocode
      // below is actual code that hopefully will work
      $uploader->upload($dir.$file,$dropboxdir.$file);
    } else {
      if(!is_link($file)) { // probably best not to follow symlinks, so we check that with is_link()
        copyDirRecursive($dir . '/' . $file); // recursion time
      }
    }

  }
}

// CALL THE FUNCTION
copyDirRecursive($dirtocopy); // you have to call a function for it to do anything

print_r($errors); // use this or var_dump($errors) to see what errors came up

Based on the code you have you want something along the lines of

require 'DropboxUploader.php';

$dirtocopy = './example_directory/';
$dropboxdir = '/backupdir/';
$uploader = new DropboxUploader('email@gmail.com', 'Password');// enter dropbox credentials

if ($handle = opendir($dirtocopy)) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {

            $uploader->upload($dirtocopy.$entry, $dropboxdir.$entry);

        }
    }
    closedir($handle);
}

I'm not 100% sure on the dropbox directory code as I've just pulled it out of your example and you may want to drop the first / in $dropboxdir. But I'm sure you can figure that out.

For reference the code for looping a directory is example #2 from http://php.net/manual/en/function.readdir.php

For recursive directory copying

require 'DropboxUploader.php';

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

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

            }
        }
        closedir($handle);
    }
}

$dirtocopy = './example_directory/';
$dropboxdir = '/backupdir/';
$uploader = new DropboxUploader('email@gmail.com', 'Password');// enter dropbox credentials

uploaddirtodropbox($dirtocopy, $dropboxdir, $uploader);

In the question you have asked for help with using this https://github.com/jakajancar/DropboxUploader/ and I have given you code to do so, however if you read the github page it says

Its development was started before Dropbox released their API, and to work, it scrapes their website. So you can and probably should use their API now.

So it might be a good idea for you to look for an alternative.

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