I think I may have found a PHP program to upload files from a specific folder onto my Dropbox account. The full solution can be found here.
The code seems to work because files and folders a like are being uploaded. However, I don't want the files on my server to be compressed beforehand: I want to copy all files with the files and folders within.
How can the code be modified please? All I want is to copy a specific directory called uploads from my server to dropbox. After modifying the code I managed to arrive at this code:
<?php
// Set the timezone so filenames are correct
date_default_timezone_set('Europe/London');
// Dropbox username/password
$dropbox_email='[email protected]';
$dropbox_pass='password';
// Filenames for backup files
$backup_files = "files_" . date("Y.m.d-h.i.s_l") . '.zip';
// File to backup
$siteroot = "/site/home/public_html/website/parent/child/uploads/";
// Backup all files in public_html apart from the gz
system("zip -r $backup_files $siteroot");
include("DropboxUploader.php");
$uploader = new DropboxUploader($dropbox_email, $dropbox_pass);
$uploader->upload($backup_files,'Backup/Files/');
system("rm $backup_files");
?>
Actual Solution Special thanks to Alireza Noori, halfer and everyone else.
<?php
// Set the timezone so filenames are correct
date_default_timezone_set('Europe/London');
// Backup all files in public_html apart from the gz
$siteroot = "/path/to/backup";
$dropbox_email='dropbox@email'; //Dropbox username
$dropbox_pass='pass'; // Dropbox password
include("DropboxUploader.php");
$uploader = new DropboxUploader($dropbox_email, $dropbox_pass);
function FolderToDropbox($dir, $dropbox_link){
$dropbox_folder = 'FolderInDropboxRoot/';
$files = scandir($dir);
foreach($files as $item){
if($item != '.' && $item != '..'){
if(is_dir($dir.'/'.$item)) FolderToDropbox($dir.'/'.$item,$dropbox_link);
else if(is_file($dir.'/'.$item)) {
$clean_dir = str_replace("/path/to/backup", "", $dir);
$dropbox_link->upload($dir.'/'.$item,$dropbox_folder.$clean_dir.'/');
}
}
}
}
FolderToDropbox($siteroot,$uploader);
?>
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='[email protected]';
$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:
system
call from your code, since you don't want to do any compression$path
in my code to $siteroot
which you use in yours$path = ...
line in my code (since you define $siteroot
yourself)$objects
line from my code after you define $siteroot
, into your code$uploader->upload()
line in your code with the for
loop I provide, removing the echo statement from my code$name
to $backup_files
in your codeJiggle it all about a bit, and it should work. Good luck, and feel free to ask questions!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With