-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcopy_progress.php
More file actions
80 lines (63 loc) · 1.83 KB
/
Copy pathcopy_progress.php
File metadata and controls
80 lines (63 loc) · 1.83 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
<?php
function copy_progress($source, $dest, $session_key = 'download', $context = NULL) {
$existing_session = FALSE;
if(!$context)
$context = stream_context_create();
$fp = fopen($source, 'r', FALSE, $context);
$out = fopen($dest, 'w', FALSE, $context);
if(!$fp) {
trigger_error('Invalid URL supplied: ' . $source, E_USER_WARNING);
return FALSE;
}
if(!$out) {
trigger_error('Unable to open output file: ' . $dest, E_USER_WARNING);
return FALSE;
}
$meta = stream_get_meta_data($fp);
foreach($meta['wrapper_data'] as $header) {
if(stripos($header, 'Content-Length') === 0) {
$parts = explode(':', $header);
if(count($parts) > 1) {
$filesize = intval(trim($parts[1]));
} else {
trigger_error('Invalid Content-Length header in response: ' . $source, E_USER_WARNING);
}
}
}
if(!$filesize) {
return FALSE;
}
if(!session_id()) {
session_start();
$existing_session = true;
}
$_SESSION[$session_key] = array(
'URL' => $source,
'TotalSize' => $filesize,
'BytesRead' => 0,
'Percent' => 0,
'Complete' => FALSE,
'Failed' => FALSE,
);
session_write_close();
while(!feof($fp)) {
$data = fread($fp, 256 * 1024); // Read in 256kb chunks
$bytes = fwrite($out, $data);
session_start();
$_SESSION[$session_key]['BytesRead'] += $bytes;
if($_SESSION[$session_key]['TotalSize'] == $_SESSION[$session_key]['BytesRead']) {
$_SESSION[$session_key]['Complete'] = TRUE;
$_SESSION[$session_key]['Percent'] = 100;
} else {
$_SESSION[$session_key]['Percent'] = $_SESSION[$session_key]['BytesRead'] / $_SESSION[$session_key]['TotalSize'] * 100;
}
session_write_close();
}
if($_SESSION[$session_key]['TotalSize'] != $_SESSION[$session_key]['BytesRead']) {
$_SESSION[$session_key]['Failed'] = TRUE;
}
if($existing_session) {
session_start();
}
return TRUE;
}