-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZipDownloader.php
executable file
·58 lines (47 loc) · 2.11 KB
/
ZipDownloader.php
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
<?php
class ZipDownloader
{
public static function temporary_zip_download($url, $temp_filename_base, $temp_path = '')
{
// path to save temporary zip file
$temporary_file_path = $target;
// construct temporary file name for downloaded zip file
$timestamp = time();
$current_year = date("Y", $timestamp);
$current_month = date("m", $timestamp);
$current_day = date("d", $timestamp);
$current_hour = date("H", $timestamp);
$current_minute = date("i", $timestamp);
$current_second = date("s", $timestamp);
$temp_file_name = $temp_filename_base . '_' . $current_year . '-' . $current_month . '-' . $current_day . '_' . $current_hour . $current_minute . $current_second;
// create handle to local temporary file copy
$temp_file_handle = fopen($temp_path . $temp_file_name, 'wb');
// prepare and execute download using curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $temp_file_handle);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);
// close file handle for temporary copy
fclose($temp_file_handle);
// check whether download was successful
$info = curl_getinfo($ch);
$download_successful = false;
if($info['http_code'] == 200) {
$download_successful = true;
$success_message = 'Download successful.';
}
else {
$success_message = 'Error: Download of ZIP file failed.';
}
// create result array
$result_array = array();
$result_array['successful'] = $download_successful;
$result_array['success_message'] = $success_message;
$result_array['temp_file_name'] = $temp_file_name;
$result_array['total_time'] = $info['total_time']; // total time for download
$result_array['size_download'] = $info['size_download']; // download size
return $result_array;
}
}
?>