douyan1613 2013-03-12 08:49
浏览 51

如何使用PHP将文件从特定目录备份到Dropbox?

I would like to create a PHP script to backup files from a particular directory on my website to my Dropbox account.

I tried to search for examples and how to work around it but I only found code to backup databases or to buy ready made solutions.

This is the code I tried

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

But instead of uploading the image from my PC to Dropbox via my website. I want to modify the above code to copy files in a specific directory on my website to Dropbox.

  • 写回答

2条回答 默认 最新

  • dtgj8529 2013-03-12 09:02
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!