1 |
<?php |
2 |
/** |
3 |
* Various imported functions, under: CC-By-SA, CC-By. |
4 |
* See each function comments for details. |
5 |
*/ |
6 |
|
7 |
/** |
8 |
* Convert size in bytes in human readable format. |
9 |
* |
10 |
* Taken from http://stackoverflow.com/a/2510459/204910 |
11 |
* licensed / CC-By-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/), |
12 |
* (c) Mef and various contributors, |
13 |
* |
14 |
* itself sourced from http://php.net/manual/en/function.filesize.php |
15 |
* so licensed under CC-By 3.0 (http://creativecommons.org/licenses/by/3.0/), |
16 |
* (c) the PHP Documentation Group |
17 |
* |
18 |
* @param integer $bytes size in bytes |
19 |
* @param integer $precision |
20 |
* |
21 |
* @return string |
22 |
*/ |
23 |
function formatBytes($bytes, $precision = 2) { |
24 |
$units = array('B', 'KB', 'MB', 'GB', 'TB'); |
25 |
|
26 |
$bytes = max($bytes, 0); |
27 |
$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); |
28 |
$pow = min($pow, count($units) - 1); |
29 |
|
30 |
// Uncomment one of the following alternatives |
31 |
// $bytes /= pow(1024, $pow); |
32 |
$bytes /= (1 << (10 * $pow)); |
33 |
|
34 |
return round($bytes, $precision) . ' ' . $units[$pow]; |
35 |
} |
36 |
|
37 |
|
38 |
/** |
39 |
* Properly dump $assoc_arr into a .ini file, with or without sections. |
40 |
* |
41 |
* Taken from http://php.net/manual/en/function.parse-ini-file.php |
42 |
* so licensed under CC-By 3.0 (http://creativecommons.org/licenses/by/3.0/), |
43 |
* (c) the PHP Documentation Group |
44 |
* |
45 |
* @param array $assoc_arr data to dump |
46 |
* @param string $path where to write the data |
47 |
* @param boolean $has_sections |
48 |
* |
49 |
* @return boolean |
50 |
*/ |
51 |
function write_ini_file($assoc_arr, $path, $has_sections = FALSE) |
52 |
{ |
53 |
$content = ''; |
54 |
if ($has_sections) { |
55 |
foreach ($assoc_arr as $key => $elem) { |
56 |
$content .= "\n;\n[" . $key . "]\n"; |
57 |
foreach ($elem as $key2 => $elem2) { |
58 |
if (is_array($elem2)) { |
59 |
for ($i = 0; $i < count($elem2); $i++) |
60 |
$content .= $key2 . '[] = "' . $elem2[$i] . "\"\n"; |
61 |
} |
62 |
elseif ( $elem2 == '') |
63 |
$content .= $key2 ." = \n"; |
64 |
else |
65 |
$content .= $key2 . ' = "' . $elem2 . "\"\n"; |
66 |
} |
67 |
} |
68 |
} else { |
69 |
foreach ($assoc_arr as $key => $elem) { |
70 |
if (is_array($elem)) { |
71 |
for ($i = 0; $i < count($elem); $i++) |
72 |
$content .= $key2."[] = \"".$elem[$i]."\"\n"; |
73 |
} |
74 |
elseif ($elem == '') |
75 |
$content .= $key2." = \n"; |
76 |
else |
77 |
$content .= $key2." = \"".$elem."\"\n"; |
78 |
} |
79 |
} |
80 |
|
81 |
if (!$handle = fopen($path, 'w')) { |
82 |
return false; |
83 |
} |
84 |
if (!fwrite($handle, $content)) { |
85 |
return false; |
86 |
} |
87 |
fclose($handle); |
88 |
|
89 |
return true; |
90 |
} |
91 |
|
92 |
|