2

我想创建一个 PHP 脚本来将文件从我网站上的特定目录备份到我的 Dropbox 帐户。

我试图搜索示例以及如何解决它,但我只找到了备份数据库或购买现成解决方案的代码。

这是我试过的代码

<?php
  $passw = "jason"; //change this to a password of your choice.
  if ($_POST) {
    require 'DropboxUploader.php';


    try {
        // Rename uploaded file to reflect original name
        if ($_FILES['file']['error'] !== UPLOAD_ERR_OK)
            throw new Exception('File was not successfully uploaded from your computer.');

        $tmpDir = uniqid('/tmpCapes/');
        if (!mkdir($tmpDir))
            throw new Exception('Cannot create temporary directory!');

        if ($_FILES['file']['name'] === "")
            throw new Exception('File name not supplied by the browser.');

        $tmpFile = $tmpDir.'/'.str_replace("/\0", '_', $_FILES['file']['name']);
        if (!move_uploaded_file($_FILES['file']['tmp_name'], $tmpFile))
            throw new Exception('Cannot rename uploaded file!');

    if ($_POST['txtPassword'] != $passw)
            throw new Exception('Wrong Password');

        // Upload
    $uploader = new DropboxUploader('user@example.com', 'password');// enter dropbox credentials
        $uploader->upload($tmpFile, $_POST['dest']);

        echo '<span style="color: green;font-weight:bold;margin-left:393px;">File successfully uploaded to my Dropbox!</span>';
    } catch(Exception $e) {
        echo '<span style="color: red;font-weight:bold;margin-left:393px;">Error: ' . htmlspecialchars($e->getMessage()) . '</span>';
    }

    // Clean up
    if (isset($tmpFile) && file_exists($tmpFile))
        unlink($tmpFile);

    if (isset($tmpDir) && file_exists($tmpDir))
        rmdir($tmpDir);
}
?>

但不是通过我的网站将图像从我的 PC 上传到 Dropbox。我想修改上面的代码,将我网站上特定目录中的文件复制到 Dropbox。

4

2 回答 2

2

你需要递归代码。

编写一个以 dir 作为参数的函数。

让它通过目录循环查看每个文件。对于每个文件,它会检查它是否是一个目录,如果不是,它会复制它。

如果它是一个目录,则该函数会调用自身。

例如

// 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
于 2013-03-16T01:46:00.387 回答
1

根据您拥有的代码,您想要一些类似的东西

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);
}

我不是 100% 确定保管箱目录代码,因为我刚刚将它从您的示例中提取出来,您可能希望将第一个/放入$dropboxdir. 但我相信你能弄清楚。

作为参考,循环目录的代码是来自http://php.net/manual/en/function.readdir.php的示例 #2

用于递归目录复制

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);

在您要求帮助使用此https://github.com/jakajancar/DropboxUploader/的问题中,我已经给了您这样做的代码,但是如果您阅读了 github 页面,它会说

它的开发是在 Dropbox 发布他们的 API 之前开始的,为了工作,它会抓取他们的网站。所以你现在可以并且可能应该使用他们的 API。

因此,寻找替代方案可能是一个好主意。

于 2013-03-12T09:02:43.347 回答