-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathremote.php
153 lines (129 loc) · 3.04 KB
/
remote.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
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
<?php
/*
* By Pedram [ Pedroxam ]
*/
header('Access-Control-Allow-Origin: *');
header('content-type: application/json; charset=utf-8');
error_reporting(0);
set_time_limit(0);
/*
* Large download file Using Curl
* if your server don't have Curl, change this function to eg: file_put_contents
*/
function largeDownload($url,$path){
$fp = fopen($path, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);
return $data;
}
/*
* Get the remote file size
* In rare cases, it does not work well, but its good.
*/
function remote_filesize($url) {
static $regex = '/^Content-Length: *+\K\d++$/im';
if (!$fp = @fopen($url, 'rb')) {
return false;
}
if (
isset($http_response_header) &&
preg_match($regex, implode("\n", $http_response_header), $matches)
) {
return (int)$matches[0];
}
return strlen(stream_get_contents($fp));
}
/*
* Readable file size
* Very Dirty function for make Readable filesize, you can change
*/
function formatSizeUnits($bytes)
{
if ($bytes >= 1073741824)
{
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
}
elseif ($bytes >= 1048576)
{
$bytes = number_format($bytes / 1048576, 2) . ' MB';
}
elseif ($bytes >= 1024)
{
$bytes = number_format($bytes / 1024, 2) . ' KB';
}
elseif ($bytes > 1)
{
$bytes = $bytes . ' Bytes';
}
elseif ($bytes == 1)
{
$bytes = $bytes . ' Bytes';
}
else
{
$bytes = '0 Bytes';
}
return $bytes;
}
/*
* Get the uploaded progress percentage
*/
function getProgress($name)
{
$log = md5($name);
$total_size = file_get_contents($log . '.txt');
$current_size = filesize($name);
$progress = ( 100 * $current_size ) / $total_size;
$progress = floor($progress);
return json_encode(array(
'progress' => $progress,
'uploaded' => formatSizeUnits($current_size),
'size' => formatSizeUnits($total_size)
));
}
/*
Start download file
*/
function downloadFile($url,$name)
{
$url = urldecode($url);
$status = true;
$put = file_put_contents(md5($name) . '.txt', remote_filesize($url));
if(!$put)
{
//$status = false;
return;
}
//Download File Can be Start from here
largeDownload($url, $name);
return json_encode(
array(
'status' => $status,
'filename' => $name
)
);
}
/*
* Get the file progress
*/
if(isset($_REQUEST['progress']) && !empty($_REQUEST['progress'])){
exit(getProgress(trim($_REQUEST['name'])));
}
$url = trim($_REQUEST['url']); // File url
$name = trim($_REQUEST['name']); // File name
if(!isset($url) && empty($name)) die();
if(!filter_var($url, FILTER_VALIDATE_URL)) {
die();
}
//$check_ext = substr($name,-3);
//$allowed = ['mkv','mp4','3gp','avi','m4v','mp3','flv','ts']; // Alowed file types
/*
* Download File
*/
//if(in_array($check_ext,$allowed)){
exit(downloadFile($url,basename($name)));
//}
?>