if file_exists filename header HTTP 404 Not Found exit fsize filesize

 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
if (!file_exists($filename)) {
header ("HTTP/1.0 404 Not Found");
exit;
}
$fsize = filesize($filename);
$ftime = date("D, d M Y H:i:s T", filemtime($filename));
$range = 0;
$handle = @fopen($filename, "rb");
if (!$handle){
header ("HTTP/1.0 403 Forbidden");
exit;
}
if ($_SERVER["HTTP_RANGE"]) {
$range = $_SERVER["HTTP_RANGE"];
$range = str_replace("bytes=", "", $range);
$range = str_replace("-", "", $range);
if ($range) {
fseek($handle, $range);
}
}
if ($range) {
header("HTTP/1.1 206 Partial Content");
} else {
header("HTTP/1.1 200 OK");
}
header("Last-Modified: {$ftime}");
header("Content-Length: ".($fsize - $range));
header("Accept-Ranges: bytes");
header("Content-Range: bytes {$range}-".($fsize - 1)."/".$fsize);
header('Cache-Control: private, max-age=3600, must-revalidate');
header('Content-Type: application/pdf');
while(!feof($handle)) {
$buf = fread($handle, 8192);
print($buf);
}
fclose($handle);