<?php
set_time_limit(0);
$allowed_url
= [
''
,
''
,
''
];
$file_urls
=
''
;
$file_url_arr
=
explode
(
','
,
$file_urls
);
$file_url_arr
=
array_unique
(
$file_url_arr
);
if
(
empty
(
$file_url_arr
)) {
$output
=
array
(
'status'
=> 2,
'code'
=> 999,
'error'
=>
'未找到合法url'
,
);
exit
(json_encode(
$output
));
}
$download_dir
= ROOT.
'download'
.DIRECTORY_SEPARATOR;
if
(!
file_exists
(
$download_dir
))
mkdir
(
$download_dir
, 0777, true);
$tmp_dir
=
$download_dir
.time().rand(100, 999).DIRECTORY_SEPARATOR;
$downloader
=
new
fileDownloader();
if
(
$file_url_arr
&& !
empty
(
$file_url_arr
))
$downloader
->download(
$tmp_dir
,
$file_url_arr
);
$file_lists
= scandir(
$tmp_dir
);
$file_lists
=
array_diff
(
$file_lists
, [
'.'
,
'..'
]);
$file_lists
=
array_values
(
$file_lists
);
if
(
empty
(
$file_lists
)){
$output
=
array
(
'status'
=> 2,
'code'
=> 999,
'error'
=>
'无下载文件'
,
);
exit
(json_encode(
$output
));
}
$file_name
=
$file_lists
[0];
$file_headers
= get_headers(
$file_urls
, 1);
header(
"Cache-Control: public"
);
header(
"Content-Description: File Transfer"
);
header(
'Content-Type: '
.
$file_headers
[
'Content-Type'
]);
header(
"Content-Transfer-Encoding: binary"
);
header(
'Content-Length: '
.
filesize
(
$tmp_dir
.
$file_name
));
$user_agent
=
$_SERVER
[
"HTTP_USER_AGENT"
];
$encoded_name
= rawurlencode(
$file_name
);
if
(preg_match(
"/Firefox/"
,
$user_agent
)) {
header(
'Content-Disposition: attachment; filename*=utf-8\'\''
.
$encoded_name
);
}
else
{
header(
'Content-Disposition: attachment; filename="'
.
$file_name
.
'"'
);
}
ob_clean();
flush
();
@readfile(
$tmp_dir
.
$file_name
);
$downloader
->deleteDir(
$tmp_dir
);
class
fileDownloader{
public
function
download(
$dir
,
$urls
=
array
()){
if
(!
file_exists
(
$dir
)) {
mkdir
(
$dir
, 0777, true);
}
if
(
empty
(
$urls
)) {
return
;
}
foreach
(
$urls
as
$val
) {
$file_name_arr
=
explode
(
'/'
,
$val
);
$file_name
=
array_pop
(
$file_name_arr
);
$ch
= curl_init();
curl_setopt(
$ch
,CURLOPT_URL,
$val
);
curl_setopt(
$ch
, CURLOPT_RETURNTRANSFER, true);
curl_setopt(
$ch
, CURLOPT_CONNECTTIMEOUT, 0);
$data
= curl_exec(
$ch
);
error_log
(var_export(curl_getinfo(
$ch
), 1));
curl_close(
$ch
);
if
(
$data
) {
file_put_contents
(
$dir
.
$file_name
,
$data
);
}
}
}
public
function
compress(
$dir
,
$filename
= false){
if
(!
file_exists
(
$dir
)) {
return
false;
}
$file_lists
= scandir(
$dir
);
$file_lists
=
array_diff
(
$file_lists
, [
'.'
,
'..'
]);
if
(
empty
(
$file_lists
)) {
return
false;
}
if
(!
$filename
) {
$filename
= time().rand(111, 999).
'.zip'
;
}
else
{
$filename
.=
'.zip'
;
}
$fullname
=
$dir
.
$filename
;
if
(!
file_exists
(
$fullname
)) {
$zip
=
new
ZipArchive();
if
(
$zip
->open(
$fullname
, ZipArchive::CREATE)==TRUE) {
foreach
(
$file_lists
as
$val
){
if
(
file_exists
(
$dir
.
$val
)){
$zip
->addFile(
$dir
.
$val
,
$val
);
}
}
$zip
->close();
}
}
return
$filename
;
}
public
function
deleteDir(
$path
)
{
if
(
is_dir
(
$path
)) {
$file_lists
= scandir(
$path
);
foreach
(
$file_lists
as
$val
) {
if
(
$val
!=
"."
&&
$val
!=
".."
) {
if
(
is_dir
(
$path
.
$val
)) {
self::deleteDir(
$path
.
$val
.
'/'
);
@
rmdir
(
$path
.
$val
.
'/'
);
}
else
{
unlink(
$path
.
$val
);
}
}
}
@
rmdir
(
$path
);
}
}
}