-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbackup.php
More file actions
executable file
·87 lines (72 loc) · 2.04 KB
/
backup.php
File metadata and controls
executable file
·87 lines (72 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
class Backup
{
private $dbxClient;
private $projectFolder;
/**
* __construct pass token and project to the client method
* @param string $token authorization token for Dropbox API
* @param string $project name of project and version
* @param string $projectFolder name of the folder to upload into
*/
public function __construct($token, $projectFolder)
{
$this->dbxClient = new Spatie\Dropbox\Client($token);
$this->projectFolder = $projectFolder;
}
/**
* upload set the file or directory to upload
* @param [type] $dirtocopy [description]
* @return [type] [description]
*/
public function upload($dirtocopy)
{
if (!file_exists($dirtocopy)) {
exit("File $dirtocopy does not exist");
} else {
//if dealing with a file upload it
if (is_file($dirtocopy)) {
$this->uploadFile($dirtocopy);
} else { //otherwise collect all files and folders
$iter = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($dirtocopy, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST,
\RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
);
//loop through all entries
foreach ($iter as $file) {
$words = explode('/',$file);
$stop = end($words);
//if file is not in the ignore list pass to uploadFile method
if (!in_array($stop, $this->ignoreList())) {
$this->uploadFile($file);
}
}
}
}
}
/**
* uploadFile upload file to dropbox using the Dropbox API
* @param string $file path to file
*/
public function uploadFile($file, $mode = 'add')
{
$path = "/".$this->projectFolder."/$file";
$contents = file_get_contents($file);
//if the contents is not empty upload otherwise do nothing
if (! empty($contents)) {
$this->dbxClient->upload($path, $contents, $mode);
}
}
/**
* ignoreList array of filenames or directories to ignore
* @return array
*/
public function ignoreList()
{
return array(
'.DS_Store',
'cgi-bin'
);
}
}