-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopy_sst.php
More file actions
226 lines (166 loc) · 6.5 KB
/
copy_sst.php
File metadata and controls
226 lines (166 loc) · 6.5 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
set_time_limit(18000); # 5 hr
define('DEBUG_MODE', true);
/**
* Class to download big files in chunks and save them in a directory. Directory : /Y-m-d/
**/
class download_class
{
public $downloadDir;
private $sourceFile;
private $destinationFileName;
private $CHUNK;
private $retry;
private $destinationFile;
private $start;
private $download_time;
# constructor
function __construct($params = array())
{
# set params
if (isset($params['sourceFile']) AND isset($params['destinationFileName']) AND isset($params['destinationPath'])) {
$this->sourceFile = $params['sourceFile'];
$this->destinationFileName = $params['destinationFileName'];
$this->downloadDir = $params['destinationPath'];
}
# set CHUNK to be downloaded
$this->CHUNK = 4194304; # 4MB at a time
}
# retrieve_remote_file_size
function retrieve_remote_file_size()
{
if (!$this->sourceFile) {
return false;
}
$ch = curl_init($this->sourceFile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
#echo $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); # get http code
curl_close($ch);
return $size;
}
# write_log
function write_log($content = '')
{
if (@DEBUG_MODE == true) {
$somecontent = "[" . date('Y-m-d H:i:s') . "] " . $content . "\n\r";
$filename = $this->downloadDir . '/log.txt';
$handle = fopen($filename, 'a');
fwrite($handle, $somecontent);
fclose($handle);
}
}
# download_file
function download_file()
{
$return = array();
if (empty($this->sourceFile) OR empty($this->destinationFileName)) {
$return['status'] = 'error';
$return['message'] = 'Invalid sourceFile OR destinationFileName';
}
$destinationFile = $this->downloadDir . '/' . $this->destinationFileName;
# Start Time Counter
$this->startTime();
try {
if (!($putData = fopen($this->sourceFile, "r")))
throw new Exception("Can't get PUT data.");
$tot_write = 0;
if (!file_exists($this->downloadDir)) {
mkdir($this->downloadDir, 0755, true); # check this on server, Server may have issue with creating dirs with specific file permissions.
}
// Create a temp file OR Make File empty if already exists
$fh = fopen($destinationFile, 'w');
fwrite($fh, '');
fclose($fh);
// Open file for writing
if (!($fp = fopen($destinationFile, "a")))
throw new Exception("Can't write to tmp file");
// Read the data a this->CHUNK at a time and write to the file
while ($data = fread($putData, $this->CHUNK)) {
$this->CHUNK_read = strlen($data);
if (($block_write = fwrite($fp, $data)) != $this->CHUNK_read)
throw new Exception("Can't write more to tmp file");
$tot_write += $block_write;
}
// Close file
if (!fclose($fp))
throw new Exception("Can't close tmp file");
unset($putData);
$remote_file_size = $this->retrieve_remote_file_size($this->sourceFile);
$file_size = filesize($destinationFile);
// Check file length
if ($file_size != $remote_file_size) {
$this->retry = $this->retry + 1;
# End Time Counter
$this->endTime();
# log
$this->write_log('error :: Failed to download' . ' :: sourceFile: ' . $this->sourceFile . ' :: Execution Time in Seconds: ' . $this->download_time);
if ($this->retry < 3) {
# Start Time Counter
$this->startTime();
# retry :: IMP = here return is neccessory else it'll not return anything to $obj method
return $this->download_file();
} else {
throw new Exception("Wrong file size, Failed to download!");
}
} else {
$this->destinationFile = $destinationFile;
# success
$return['status'] = 'success';
$return['message'] = $destinationFile;
# End Time Counter
$this->endTime();
$this->write_log($return['status'] . ' :: ' . $return['message'] . ' :: Download Time in Seconds: ' . $this->download_time);
}
}
catch (Exception $e) {
#echo '', $e->getMessage(), "\n";
$return['status'] = 'error';
$return['message'] = $e->getMessage();
# End Time Counter
$this->endTime();
$this->write_log($return['status'] . ' :: ' . $return['message'] . ' :: sourceFile: ' . $this->sourceFile . ' :: Execution Time in Seconds: ' . $this->download_time);
}
return $return;
}
function startTime()
{
# count download startTime
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$this->start = $time;
}
function endTime()
{
if ($this->start) {
# count download endTime
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$download_time = ($finish - $this->start);
$this->download_time = round($download_time, 3); // For log.
}
}
}
$params = Array();
$params['sourceFile'] = 'https://xtomedicalhall.com/xtn_backup-21-9-21.zip';
$params['destinationFileName'] = 'xtn_backup-21-9-21.zip';
$params['destinationPath'] = dirname(__FILE__) . '/'; #Current folder with a slash '/'
$obj = new download_class($params);
if(!file_exists($params['destinationPath'].$params['destinationFileName'] ))
{
$result = $obj->download_file();
echo "File migrated successfully. Have Fun!";
}
else
{
echo "File already exists";
}