-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
77 lines (68 loc) · 1.86 KB
/
functions.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
<?php
/**
* Created by PhpStorm.
* User: xubandit
* Date: 15/7/6
* Time: 上午11:13
*/
function get_post($key){
return isset($_GET[$key])?$_GET[$key]:(isset($_POST[$key]) ?$_POST[$key]:'');
}
function json_output($status,$message='',$data=array()){
header('Content-type: application/json');
echo json_encode(array('errno'=>$status,'msg'=>$message,'data'=>$data));
return true;
}
function human_filesize($bytes, $decimals = 2) {
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}
if(!function_exists('array_column')){
function array_column($array,$col_name){
$result = array();
if(empty($array)){return $result;}
foreach($array as $item){
$result = isset($item[$col_name]) ? $item[$col_name] :null;
}
return $result;
}
}
function log_debug($message){
is_string($message) or $message=json_encode($message);
$message = date('H:i:s').' --> '.$message.PHP_EOL;
$log_file = LOG_PATH.'/'.date('Ymd').'.php';
return file_put_contents($log_file,$message,FILE_APPEND);
}
function site_url($param){
return 'http://test.centos65.home/baidu_pcs_ui/index.php?'.http_build_query($param);
}
/**
* @param $progress
* @return float|int
* @desc 获取下载进度百分比
*/
function get_progress_rate($progress){
$progress = explode('/',trim($progress));
if(count($progress) !=2){
return 0;
}
$down_size = human_filesize_to_number($progress[0]);
$full_size = human_filesize_to_number($progress[1]);
if($full_size){
$rate = round($down_size/$full_size,2)*100;
}else{
$rate=0;
}
return $rate;
}
function human_filesize_to_number($size){
$units = array('B'=>0, 'KB'=>1, 'MB'=>2, 'GB'=>3, 'TB'=>4);
$is_match = preg_match('/[a-zA-Z]+/',$size,$match);
if($is_match){
$size = ((int)$size)*pow(1024,$units[strtoupper($match[0])]);
}else{
$size = 0;
}
return $size;
}