This question already has an answer here:
这个问题已经有了答案:
- How to backup files from a specific directory to Dropbox using PHP only? 3 answers
- 如何使用PHP从特定目录备份文件到Dropbox ?3答案
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.
我想我可能已经找到了一个PHP程序,可以将文件从一个特定的文件夹上传到我的Dropbox账户上。完整的解可以在这里找到。
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.
这段代码似乎可以工作,因为文件和文件夹a正在被上载。但是,我不希望服务器上的文件预先被压缩:我希望将所有文件与其中的文件和文件夹一起复制。
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:
请问如何修改代码?我只想从我的服务器上拷贝一个名为uploadload的特定目录到dropbox上。修改了代码后,我终于达到了这个代码:
<?php
// Set the timezone so filenames are correct
date_default_timezone_set('Europe/London');
// Dropbox username/password
$dropbox_email='dropbox@dropbox.com';
$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.
真正的解决方案特别感谢Alireza Noori, halfer和其他人。
<?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);
?>
3 个解决方案
#1
3
What @halfer is suggesting is this (I just modified your potential solution based on his idea) so he should take credit:
@halfer的意思是(我刚刚根据他的想法修改了你的潜在解决方案),所以他应该相信:
<?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?
顺便说一下,上面的功能是从这里开始的:如何使用PHP从特定目录备份文件到Dropbox ?
#2
2
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站点的代码片段。它所做的就是获取一个目录路径(作为字符串)并输出其中所有文件的完整路径名(作为若干字符串)。这是非常有用的,因为我们可以利用这一功能对文件进行个性化处理(比如将它们上传到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:
因此,既然您想学习PHP,既然我给您一个现成的解决方案,您将不会学到任何东西:-),尝试以下方法:
- Remove the
system
call from your code, since you don't want to do any compression - 从代码中删除系统调用,因为您不想进行任何压缩
- Get the snippet I've provided working on your machine (it will work on its own), changing '/etc' to whatever path you want
- 获取我提供的在您的机器上工作的代码片段(它将自己工作),将'/etc'更改为您想要的任何路径
- Change
$path
in my code to$siteroot
which you use in yours - 将我代码中的$path更改为您在您的代码中使用的$siteroot
- Remove the
$path = ...
line in my code (since you define$siteroot
yourself) - 删除$path =…我的代码中的行(因为您自己定义了$siteroot)
- Test the snippet again
- 再次测试代码片段
- Add in the
$objects
line from my code after you define$siteroot
, into your code - 在代码中定义$siteroot之后,从我的代码中添加$objects行
- Wrap the
$uploader->upload()
line in your code with thefor
loop I provide, removing the echo statement from my code - 在代码中使用我提供的for循环包装$uploader->upload()行,从代码中删除echo语句
- Change
$name
to$backup_files
in your code - 将代码中的$name更改为$backup_files
Jiggle it all about a bit, and it should work. Good luck, and feel free to ask questions!
稍微摇晃一下,就可以了。祝你好运,欢迎提问!
#3
0
What about using the command line version of dropbox? http://www.dropboxwiki.com/Using_Dropbox_CLI
使用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.
把文件复制到dropbox文件夹,让deamon做它需要做的事情。
#1
3
What @halfer is suggesting is this (I just modified your potential solution based on his idea) so he should take credit:
@halfer的意思是(我刚刚根据他的想法修改了你的潜在解决方案),所以他应该相信:
<?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?
顺便说一下,上面的功能是从这里开始的:如何使用PHP从特定目录备份文件到Dropbox ?
#2
2
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站点的代码片段。它所做的就是获取一个目录路径(作为字符串)并输出其中所有文件的完整路径名(作为若干字符串)。这是非常有用的,因为我们可以利用这一功能对文件进行个性化处理(比如将它们上传到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:
因此,既然您想学习PHP,既然我给您一个现成的解决方案,您将不会学到任何东西:-),尝试以下方法:
- Remove the
system
call from your code, since you don't want to do any compression - 从代码中删除系统调用,因为您不想进行任何压缩
- Get the snippet I've provided working on your machine (it will work on its own), changing '/etc' to whatever path you want
- 获取我提供的在您的机器上工作的代码片段(它将自己工作),将'/etc'更改为您想要的任何路径
- Change
$path
in my code to$siteroot
which you use in yours - 将我代码中的$path更改为您在您的代码中使用的$siteroot
- Remove the
$path = ...
line in my code (since you define$siteroot
yourself) - 删除$path =…我的代码中的行(因为您自己定义了$siteroot)
- Test the snippet again
- 再次测试代码片段
- Add in the
$objects
line from my code after you define$siteroot
, into your code - 在代码中定义$siteroot之后,从我的代码中添加$objects行
- Wrap the
$uploader->upload()
line in your code with thefor
loop I provide, removing the echo statement from my code - 在代码中使用我提供的for循环包装$uploader->upload()行,从代码中删除echo语句
- Change
$name
to$backup_files
in your code - 将代码中的$name更改为$backup_files
Jiggle it all about a bit, and it should work. Good luck, and feel free to ask questions!
稍微摇晃一下,就可以了。祝你好运,欢迎提问!
#3
0
What about using the command line version of dropbox? http://www.dropboxwiki.com/Using_Dropbox_CLI
使用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.
把文件复制到dropbox文件夹,让deamon做它需要做的事情。