forked from diversen/http-send-file
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendfile.php
More file actions
194 lines (169 loc) · 5.49 KB
/
Copy pathsendfile.php
File metadata and controls
194 lines (169 loc) · 5.49 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
<?php
namespace shemgp;
/**
* Sends a file to a client, with support for (multiple) range requests.
* It is also able to throttle the download.",
*/
class sendfile
{
//public
/**
* if false we set content disposition from file that will be sent
* @var mixed $disposition
*/
private $disposition = false;
/**
* throttle speed in seconds
* Defaults to no throttle
* @var float $sec
*/
private $sec = 0;
/**
* bytes per $sec
* @var int $bytes
*/
private $bytes = 40960;
/**
* if contentType is false we try to guess it
* @var mixed $contentType
*/
private $type = false;
/**
* set content disposition
* @param type $file_name
*/
public function contentDisposition ($file_name = false) {
$this->disposition = $file_name;
}
/**
* set throttle speed
* @param float $sec
* @param int $bytes
*/
public function throttle ($sec = 0.1, $bytes = 40960) {
$this->sec = $sec;
$this->bytes = $bytes;
}
/**
* set content mime type if false we try to guess it
* @param string $content_type
*/
public function contentType ($content_type = null) {
$this->type = $content_type;
}
/**
* get name from path info
* @param type $file
* @return type
*/
private function name ($file) {
$info = pathinfo($file);
return $info['basename'];
}
/**
* Sets-up headers and starts transfering bytes
*
* @param string $file_path
* @param boolean $withDisposition
* @throws Exception
*/
public function send($file_path, $withDisposition=TRUE, $asDownload = TRUE) {
if (!is_readable($file_path)) {
throw new \Exception('File not found or inaccessible!');
}
$size = filesize($file_path);
if (!$this->disposition) {
$this->disposition = $this->name($file_path);
}
if (!$this->type) {
$this->type = $this->getContentType($file_path);
}
//turn off output buffering to decrease cpu usage
$this->cleanAll();
// required for IE, otherwise Content-Disposition may be ignored
if (ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'Off');
}
$contentDisposition = 'download';
if (!$asDownload)
$contentDisposition = 'inline';
header('Content-Type: ' . $this->type);
if ($withDisposition) {
header('Content-Disposition: ' . $contentDisposition . '; filename="' . $this->disposition . '"');
}
header('Accept-Ranges: bytes');
// The three lines below basically make the
// download non-cacheable
header("Cache-control: private");
header('Pragma: private');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// multipart-download and download resuming support
if (isset($_SERVER['HTTP_RANGE'])) {
list($a, $range) = explode("=", $_SERVER['HTTP_RANGE'], 2);
list($range) = explode(",", $range, 2);
list($range, $range_end) = explode("-", $range);
$range = intval($range);
if (!$range_end) {
$range_end = $size - 1;
} else {
$range_end = intval($range_end);
}
$new_length = $range_end - $range + 1;
header("HTTP/1.1 206 Partial Content");
header("Content-Length: $new_length");
header("Content-Range: bytes $range-$range_end/$size");
} else {
$new_length = $size;
header("Content-Length: " . $size);
}
/* output the file itself */
$chunksize = $this->bytes; //you may want to change this
$bytes_send = 0;
$file = @fopen($file_path, 'rb');
if ($file) {
if (isset($_SERVER['HTTP_RANGE'])) {
fseek($file, $range);
}
while (!feof($file) && (!connection_aborted()) && ($bytes_send < $new_length) ) {
$buffer = fread($file, $chunksize);
echo($buffer); //echo($buffer); // is also possible
flush();
usleep($this->sec * 1000000);
$bytes_send += strlen($buffer);
}
fclose($file);
} else {
throw new \Exception('Error - can not open file.');
}
}
/**
* method for getting mime type of a file
* @param string $path
* @return string $mime_type
*/
private function getContentType($path) {
$result = false;
if (is_file($path) === true) {
if (function_exists('finfo_open') === true) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
if (is_resource($finfo) === true) {
$result = finfo_file($finfo, $path);
}
finfo_close($finfo);
} else if (function_exists('mime_content_type') === true) {
$result = preg_replace('~^(.+);.*$~', '$1', mime_content_type($path));
} else if (function_exists('exif_imagetype') === true) {
$result = image_type_to_mime_type(exif_imagetype($path));
}
}
return $result;
}
/**
* clean all buffers
*/
private function cleanAll() {
while (ob_get_level()) {
ob_end_clean();
}
}
}