download.php ➔ Download()   B
last analyzed

Complexity

Conditions 10
Paths 52

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 52
nop 3
dl 0
loc 39
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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()) {
0 ignored issues
show
Deprecated Code introduced by
The function NixOs() has been deprecated with message: Use Fwlib\Util\Env::isNixOs()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function DownloadFile() has been deprecated with message: Use Fwlib\Util\HttpUtil::downloadFile()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function BaseName1() has been deprecated with message: Use native basename()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
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
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

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.

Loading history...
125