1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package fwolflib |
4
|
|
|
* @subpackage func |
5
|
|
|
* @copyright Copyright 2007-2012, Fwolf |
6
|
|
|
* @author Fwolf <[email protected]> |
7
|
|
|
* @since 2007-04-29 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
require_once(dirname(__FILE__) . '/../fwolflib.php'); |
12
|
|
|
require_once(FWOLFLIB . 'func/env.php'); |
13
|
|
|
require_once(FWOLFLIB . 'func/filesystem.php'); |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Download content as a file |
18
|
|
|
* |
19
|
|
|
* @deprecated Use Fwlib\Util\HttpUtil::download() |
20
|
|
|
* @param string $content Content to download |
21
|
|
|
* @param string $filename Download file name, send to client, not path on server. |
22
|
|
|
* @param string $mime Mime type of file |
23
|
|
|
* @return boolean |
24
|
|
|
*/ |
25
|
|
|
function Download ($content, $filename = '', $mime = 'application/force-download') { |
26
|
|
|
list($usec, $sec) = explode(" ", microtime()); |
27
|
|
|
$usec = substr(strval($usec), 2, 3); |
28
|
|
|
$tmpfilename = $sec . $usec; |
29
|
|
|
|
30
|
|
|
if (empty($filename)) { |
31
|
|
|
// Use timestamp as filename if not provide |
32
|
|
|
$filename = $tmpfilename; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (NixOs()) { |
36
|
|
|
$filepath = '/tmp/'; |
37
|
|
|
} else { |
38
|
|
|
$s_tmp = ''; |
39
|
|
|
if (!empty($_ENV["TEMP"])) |
40
|
|
|
$s_tmp = $_ENV["TEMP"]; |
41
|
|
|
if (empty($s_tmp) && !empty($_ENV["TMP"])) |
42
|
|
|
$s_tmp = $_ENV["TMP"]; |
43
|
|
|
// Default, this should never accur |
44
|
|
|
if (empty($s_tmp)) |
45
|
|
|
$s_tmp = 'c:/windows/temp/'; |
46
|
|
|
// And check again |
47
|
|
|
if (!is_dir($s_tmp) || !is_writable($s_tmp)) |
48
|
|
|
die('No temp dir to store file content which need to downloaded.'); |
49
|
|
|
|
50
|
|
|
$filepath = $s_tmp; |
51
|
|
|
} |
52
|
|
|
// Add the ending '/' to tmp path |
53
|
|
|
if ('/' != substr($filepath, -1)) |
54
|
|
|
$filepath .= '/'; |
55
|
|
|
// Then got full path of tmp file |
56
|
|
|
$tmpfilename = $filepath . $tmpfilename; |
57
|
|
|
|
58
|
|
|
file_put_contents($tmpfilename, $content); |
59
|
|
|
$result = DownloadFile($tmpfilename, $filename, $mime); |
60
|
|
|
|
61
|
|
|
unlink($tmpfilename); |
62
|
|
|
return $result; |
63
|
|
|
} // end of func Download |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Download a file |
68
|
|
|
* |
69
|
|
|
* @deprecated Use Fwlib\Util\HttpUtil::downloadFile() |
70
|
|
|
* @param string $filepath Full path to download file. |
71
|
|
|
* @param string $filename Download file name, send to client, not path on server. |
72
|
|
|
* @param string $mime Mime type of file |
73
|
|
|
* @return boolean |
74
|
|
|
*/ |
75
|
|
|
function DownloadFile ($filepath, $filename = '', $mime = 'application/force-download') { |
76
|
|
|
// Check and fix parameters |
77
|
|
|
if (!is_file($filepath) || !is_readable($filepath)) |
78
|
|
|
return false; |
79
|
|
|
// If no client filename given, use original name |
80
|
|
|
if (empty($filename)) |
81
|
|
|
$filename = BaseName1($filepath); |
82
|
|
|
|
83
|
|
|
// Begin writing headers |
84
|
|
|
header("Cache-Control:"); |
85
|
|
|
header("Cache-Control: public"); |
86
|
|
|
|
87
|
|
|
//Use the switch-generated Content-Type |
88
|
|
|
header("Content-Type: $mime"); |
89
|
|
|
|
90
|
|
|
// workaround for IE filename bug with multiple periods / multiple dots in filename |
91
|
|
|
// that adds square brackets to filename - eg. setup.abc.exe becomes setup[1].abc.exe |
92
|
|
|
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) |
93
|
|
|
// count is reference (&count) in str_replace, so can't use it. |
94
|
|
|
$filename = preg_replace('/\./', '%2e', $filename, substr_count($filename, '.') - 1); |
95
|
|
|
//$iefilename = preg_replace('/\./', '%2e', $filename, substr_count($filename, '.') - 1); |
96
|
|
|
|
97
|
|
|
header("Content-Disposition: attachment; filename=\"$filename\""); |
98
|
|
|
//header("Content-Range: $from-$to fsize"); 加上压缩包头信息不正确 |
99
|
|
|
//header("Content-Length: $content_size"); 加上压缩包头信息不正确 |
100
|
|
|
|
101
|
|
|
header("Accept-Ranges: bytes"); |
102
|
|
|
|
103
|
|
|
// Read temp file & output |
104
|
|
|
$size = filesize($filepath); |
105
|
|
|
$size_downloaded = 0; // Avoid infinite loop |
106
|
|
|
$size_step = 1024 * 8; // Control download speed |
107
|
|
|
|
108
|
|
|
$fp = fopen($filepath, "rb"); |
109
|
|
|
//fseek($fp,$range); |
110
|
|
|
// Start buffered download |
111
|
|
|
//reset time limit for big files |
112
|
|
|
set_time_limit(0); |
113
|
|
|
while(!feof($fp) && ($size > $size_downloaded)) { |
114
|
|
|
print(fread($fp, $size_step)); |
115
|
|
|
$size_downloaded += $size_step; |
116
|
|
|
//flush(); 这个是多余的函数,加上会使压缩包下载不完整 |
117
|
|
|
//ob_flush(); 这个也是多余的函数,加上会使压缩包下载不完整 |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
fclose($fp); |
121
|
|
|
//unlink($ft_name); |
122
|
|
|
return true; |
123
|
|
|
} // end of func DownloadFile |
124
|
|
|
?> |
|
|
|
|
125
|
|
|
|
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.
A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.