<?php
<liu21st@gmail.com>
function createRandom($length, $chars = '0123456789')
{
$hash = '';
$max = strlen($chars) - 1;
for ($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
function createRandomStr($lenth = 6)
{
return createRandom($lenth, '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ');
}
function createPassword($password, $encrypt = '')
{
$pwd = [];
$pwd['encrypt'] = $encrypt ? $encrypt : createRandomStr();
$pwd['password'] = md5(md5(trim($password)) . $pwd['encrypt']);
return $encrypt ? $pwd['password'] : $pwd;
}
function delDir($dirpath){
$dh=opendir($dirpath);
while (($file=readdir($dh))!==false) {
if($file!="." && $file!="..") {
$fullpath=$dirpath."/".$file;
if(!is_dir($fullpath)) {
unlink($fullpath);
} else {
delDir($fullpath);
@rmdir($fullpath);
}
}
}
closedir($dh);
$isEmpty = true;
$dh=opendir($dirpath);
while (($file=readdir($dh))!== false) {
if($file!="." && $file!="..") {
$isEmpty = false;
break;
}
}
return $isEmpty;
}
function LogWirte($field , $Astring){
$path = $_SERVER['DOCUMENT_ROOT'];
$path = $path."/data/log/".$field.'/';
$file = $path."log".date('Ymd',time()).".txt";
if(!is_dir($path)){ mkdir($path,0777,true); }
$LogTime = date('Y-m-d H:i:s',time());
if(!file_exists($file))
{
$logfile = fopen($file, "w") or die("Unable to open file!");
fprintf($logfile, "[$LogTime]:".json_encode($Astring,JSON_UNESCAPED_UNICODE)."\r\n");
fclose($logfile);
}else{
$logfile = fopen($file, "a") or die("Unable to open file!");
@fprintf($logfile, "[$LogTime]:".json_encode($Astring,JSON_UNESCAPED_UNICODE)."\r\n");
fclose($logfile);
}
}