forked from Centrallatice/jDownload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jdownload.php
executable file
·114 lines (86 loc) · 2.72 KB
/
jdownload.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
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
<?php
// initialise functions
if($_GET['action'] && $_GET['path']) {
// Append full document root to provided file path
$file_path = $_SERVER['DOCUMENT_ROOT'].$_GET['path'];
if(!file_exists($file_path) || !is_file($file_path)) {
echo "error";
// Quit
} else {
// fetch Mime type here so avialable to both functions
$mime_types=array(
"pdf" => "application/pdf",
"txt" => "text/plain",
"html" => "text/html",
"htm" => "text/html",
"zip" => "application/zip",
"doc" => "application/msword",
"xls" => "application/vnd.ms-excel",
"ppt" => "application/vnd.ms-powerpoint",
"gif" => "image/gif",
"png" => "image/png",
"jpeg"=> "image/jpg",
"jpg" => "image/jpg",
);
// Get the extension of the file
$ext = substr($file_path, strrpos($file_path, '.') + 1);
if(!array_key_exists($ext, $mime_types)){
echo json_encode(array('error'=>'denied'));
die;
}
// Remove null bytes
$file_path = str_replace("\0", "", $file_path);
$file_path = str_replace("%00", "", $file_path);
// call appropriate function
switch($_GET['action']) {
case "download":
get_file($file_path);
break;
case "info":
get_info($file_path);
break;
default:
echo 'error';
break;
}
}
}
// This function checks for the file, checks to see if can be opened and then forces the file to the browser
function get_file($file_path){
global $mime_type, $ext;
// Check if mimetypes exists in our list
$content_type = array_key_exists($ext, $mime_types) ? $mime_types[$ext] : "application/force-download";
// Turn off gzip for IE browsers
if(ini_get('zlib.output_compression')){
ini_set('zlib.output_compression', 'Off');
}
// Set headers to force file download
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, pre-check=0");
header("Content-Disposition: attachment; filename=".basename($file_path)."");
header("Content-Type: ".$content_type);
header("Content-Transfer-Encoding: binary");
header("Content-Length: ". filesize($file_path));
// Discards the contents of the output buffer
ob_clean();
flush();
// Read the file
readfile($file_path);
// Exit
exit;
}
function get_info($file_path) {
global $mime_types, $ext;
// get file info
$filename = basename($file_path);
$filetype = array_key_exists($ext, $mime_types) ? $mime_types[$ext] : "Unknown";
$filesize = round(filesize($file_path) / 1024); // file size in KB
$data = array(
"filename" => $filename,
"filetype" => $filetype,
"filesize" => $filesize,
);
echo json_encode($data);
}
?>