// +----------------------------------------------------------------------
// | 常用函数库
// +----------------------------------------------------------------------
/**
* 字符串截取,支持中文和其他编码
* @static
* @access public
* @param string $str 需要转换的字符串
* @param string $start 开始位置
* @param string $length 截取长度
* @param string $charset 编码格式
* @return string
*/
function msubstr($str, $start = 0, $length, $charset = "utf-8", $suff = true) {
if (function_exists("mb_substr")) {
$slice = mb_substr($str, $start, $length, $charset);
} elseif (function_exists('iconv_substr')) {
$slice = iconv_substr($str, $start, $length, $charset);
} else {
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("", array_slice($match[0], $start, $length));
}
if (mb_strlen($str, 'utf8') > $length && $suff) {
return $slice . '...';
} else {
return $slice;
}
}
function clear_tag($str) {
$str = preg_replace('/<t(.+?)>/', '', $str);
$str = preg_replace('/<\/t(.+?)>/', '', $str);
$str = preg_replace('/<iframe(.+?)><\/iframe>/', '', $str);
$str = preg_replace('/<script(.+?)><\/script>/', '', $str);
$str = preg_replace('/<div (.+?)>/', '', $str);
$str = preg_replace('/<a(.+?)>/', '', $str);
$str = preg_replace('/<(.+?)>/', '', $str);
$str = preg_replace('/<img(.+?)>/', '', $str);
$str = preg_replace('/ /', '', $str);
return $str;
}
/**
* js返回跳转
* @access public
* @param string $url 地址
* @return string
*/
function back($url = "") {
if (!empty($url)) {
return "javascript:location.href='{$url}'";
} else {
return "javascript:history.go(-1);";
}
}
/**
* 获取UUID
* @access public
* @param null
* @return string
*/
function guid() {
$_min = M()->query('select uuid() as uuid');
if (!empty($_min[0]['uuid'])) {
return $_min[0]['uuid'];
}
}
/**
* 格式化的打印
* @param array $arr
* @return array
*/
function print_g($arr = array(), $is_exit = true) {
echo '<pre>';
print_r($arr);
echo '</pre>';
if ($is_exit) {
exit;
}
}
//简单的手机加解密方法
function mobileEncode($num, $type = 'encode') {
$saltArr = array(
2245212323, 6572349096, 3590886743, 4568764543, 8335178900,
7456233435, 3890778731, 1536258655, 1524859696, 4515298563,
);
if ($type == 'encode') {
$idx = substr($num, -1, 1);
} else {
$idx = substr($num, -1, 1);
$last = substr($num, -2, 1);
$idx = $idx >= $last ? $idx - $last : ($idx + 10) - $last;
}
$num = substr($num, 0, strlen($num) - 1);
$salt = $saltArr[$idx];
//判断是否符合加密条件
if (is_numeric($num) && strlen($num) == 10) {
$numArr = str_split($num);
for ($i = 0; $i < count($numArr); $i++) {
if ($type == 'encode') {
if ($i == 9) {
$last = $numArr[$i] + $idx;
array_push($numArr, $last > 9 ? substr($last, 1, 1) : substr($last, 0, 1));
}
$n = $numArr[$i] + substr($salt, $i, 1);
$n = $n > 9 ? substr($n, 1, 1) : substr($n, 0, 1);
} else if ($type == 'decode') {
if ($numArr[$i] >= substr($salt, $i, 1)) {
$n = $numArr[$i] - substr($salt, $i, 1);
} else {
$n = ($numArr[$i] + 10) - substr($salt, $i, 1);
}
if ($i == 9) {
array_push($numArr, $idx);
}
}
if ($i < 9) {
$numArr[$i] = $n;
}
}
return join($numArr, '');
}
return 'invalid';
}
//设置金额格式
function setMoney($m) {
return number_format($m, 2, '.', ',');
}
//1:证件号,2:手机号,3:QQ号,4流水号,5邮箱
function hiddenCode($str = '', $mod = 2) {
if (empty($str)) {
return '';
}
if ($mod == 1) {
$hc = half_replace($str, 3, 4);
} else if ($mod == 2) {
$hc = substr($str, 0, 3) . '*****' . substr($str, -3);
} else if ($mod == 3) {
$hc = half_replace($str, 0, 4);
} else if ($mod == 4) {
$hc = '***' . substr($str, -6, 6);
} else if ($mod == 5) {
$hc = substr($str, 0, 1) . '*****' . substr($str, strripos($str, '@') - 1);
} else {
$hc = half_replace($str, 0, 2);
}
return $hc;
}
//隐藏替换
function half_replace($str, $fNum = 3, $bNum = 4) {
$len = strlen($str) - ($fNum + $bNum);
return substr_replace($str, str_repeat('*', $len), $fNum, $len);
}
//隐藏中文姓名
function showName($name) {
if (empty($name)) {
return '';
}
$encode = mb_detect_encoding($name);
$num = mb_strlen($name, $encode);
if ($num == 2) {
$out = mb_substr($name, 0, 1, $encode) . '*';
} else if ($num == 3) {
$out = mb_substr($name, 0, 1, $encode) . '**';
} else if ($num == 4) {
$out = mb_substr($name, 0, 2, $encode) . '**';
} else if (strlen($name) <= 3) {
$out = substr($name, 0, 1) . '*';
} else {
$out = mb_substr($name, 0, 4, $encode) . '***';
}
return $out;
}
/**
* 判断图片是否存在
* @param [type] $url [远程的图片路径]
* @return [boolean] [判断图片是否存在 true 不]
*/
function img_exits($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1); // 不下载
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$contents = curl_exec($ch);
if (preg_match("/404/", $contents)) {
return false;
} else {
return true;
}
}
/**
* 模拟post/get进行url请求
*
* @since 2016-06-03 Fri 09:37:53
* @author PHPJungle
* @param string $url
* @param mix $param [array or string]
* @param bool $is_post [default:post ,false:get]
* @return string
* @abstract <pre>
* 方法说明:为了保证和以前使用方法兼容,故将$is_post默认值为true,如果需要get请求,将其置为false即可
*/
function request_post($url = '', $param = '', $is_post = true) {
$url = trim($url);
if (empty($url)) {
return false;
}
$queryStr = '';
if (is_array($param)) {
foreach ($param as $k => $v) {
$v = trim($v);
if ('' === $v) {
unset($param[$k]);
}
}
$queryStr = http_build_query($param); # 代码优化,减少网络开支
} else {
$queryStr = trim($param);
}
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_TIMEOUT, 40); //执行超时时间 秒
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 40); //链接超时时间 秒
if ($is_post) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $queryStr);
} else {
empty($queryStr) or $url .= '?' . $queryStr;
}
curl_setopt($ch, CURLOPT_URL, $url);
# 设置代理
if (isset($_GET['proxy']) and in_array(C('ENVIRONMENT'), array('dev', 'test'))) {
$host = trim(htmlspecialchars($_GET['host']), ENT_QUOTES) ?: 'localhost';
$port = trim(htmlspecialchars($_GET['port'], ENT_QUOTES)) ?: '7777';
{
curl_setopt($ch, CURLOPT_PROXY, $host);
curl_setopt($ch, CURLOPT_PROXYPORT, $port);}
}
$data = curl_exec($ch); //运行curl
curl_close($ch);
return $data;
}
/**
* 格式化价格
* @param [type] $price [要格式的价格数字]
* @param boolean $mode [-2:11,000.00带千分符 , -1:11000 , 0:11000.00 , 1:1.1万 , 2:1.1 , (3:300元强转万单位 0.03万 , 4:强转不带万单位)]
* @return [string] [显示小数点]
*/
function priceFormat($price, $mode = 1) {
if (($price >= 10000 || $mode >= 3) && $mode > 0) {
return number_format($price / 10000, 2, '.', '') . ($mode > 1 && $mode == 4 ? '' : '万');
} else {
if ($mode == -1) {
return number_format($price, 2, '.', '');
} else if ($mode == -2) {
return number_format($price, 2, '.', ',');
} else if ($mode == -3) {
return number_format($price / 10000, 2, '.', '');
} else if ($mode == -4) {
return number_format($price / 10000, 2, '.', '') . '万';
} else {
return number_format($price, 2, '.', '');
}
}
}
/**
* 根据开始时间和结束时间,显示进度
* @param [type] $startTime [开始时间]
* @param [type] $endTime [结束时间]
* @return [type] [显示百分比]
*/
function getBarProgress($startTime, $endTime) {
$startTime = strtotime($startTime);
$endTime = strtotime($endTime);
if ($startTime > time()) {
$progress = 0;
} else {
$allpro = $endTime - $startTime;
$cupro = time() - $startTime; // 特卖已经开始的时间
$progress = intval((($allpro - $cupro) / $allpro) * 100);
if ($progress < 0) {
$progress = 0;
}
}
return $progress;
}
/**
* 生成itemId
* @return string
*/
function getItemId() {
$hour = date('z') * 24 + date('H');
$hour = str_repeat('0', 4 - strlen($hour)) . $hour;
// echo date('y') . $hour . PHP_EOL;
return date('y') . $hour . getRandNumber(10);
}
/**
* 生成固定长度的随机数
*
* @param int $length
* @return string
*/
function getRandNumber($length = 6) {
$num = '';
if ($length >= 10) {
$t = intval($length / 9);
$tail = $length % 9;
for ($i = 1; $i <= $t; $i++) {
$num .= substr(mt_rand('1' . str_repeat('0', 9), str_repeat('9', 10)), 1);
}
$num .= substr(mt_rand('1' . str_repeat('0', $tail), str_repeat('9', $tail + 1)), 1);
return $num;
} else {
return substr(mt_rand('1' . str_repeat('0', $length), str_repeat('9', $length + 1)), 1);
}
}
/**
* 解码JS escape编码的汉字
* @param $str
* @privilege Backend:
* @return string
*/
function js_unescape($str) {
$ret = '';
$len = strlen($str);
for ($i = 0; $i < $len; $i++) {
if ($str[$i] == '%' && $str[$i + 1] == 'u') {
$val = hexdec(substr($str, $i + 2, 4));
if ($val < 0x7f) {
$ret .= chr($val);
} else if ($val < 0x800) {
$ret .= chr(0xc0 | ($val >> 6)) . chr(0x80 | ($val & 0x3f));
} else {
$ret .= chr(0xe0 | ($val >> 12)) . chr(0x80 | (($val >> 6) & 0x3f)) . chr(0x80 | ($val & 0x3f));
}
$i += 5;
} else if ($str[$i] == '%') {
$ret .= urldecode(substr($str, $i, 3));
$i += 2;
} else {
$ret .= $str[$i];
}
}
return $ret;
}
/**
* 替换PHP中的换行符
* @param string $str [要替换的内容]
* @return [type] [description]
*/
function nbrl($str = '') {
return str_replace(array("\r\n", "\r", "\n"), "<br/>", $str);
}
/**
* 获取客户端浏览器类型
* @param string $glue 浏览器类型和版本号之间的连接符
* @return string|array 传递连接符则连接浏览器类型和版本号返回字符串否则直接返回数组 false为未知浏览器类型
*/
function get_client_browser($glue = '-v') {
$browser = array();
$agent = $_SERVER['HTTP_USER_AGENT']; //获取客户端信息
/* 定义浏览器特性正则表达式 */
$regex = C('Validator.browser');
foreach ($regex as $type => $reg) {
preg_match($reg, $agent, $data);
if (!empty($data) && is_array($data)) {
$browser = $type === 'safari' ? array($data[2], $data[1]) : array($data[1], $data[2]);
break;
}
}
return empty($browser) ? false : (is_null($glue) ? $browser : implode($glue, $browser));
}
//获取访问者的操作系统
function get_client_os() {
$os = 'other';
$userAgent = strtolower($_SERVER["HTTP_USER_AGENT"]);
if ($re = strripos($userAgent, 'iphone')) {
$os = 'iphone';
} else if ($re = strripos($userAgent, 'android')) {
$os = 'android';
} else if ($re = strripos($userAgent, 'micromessenger')) {
$os = 'weixin';
} else if ($re = strripos($userAgent, 'ipad')) {
$os = 'ipad';
} else if ($re = strripos($userAgent, 'ipod')) {
$os = 'ipod';
} else if ($re = strripos($userAgent, 'windows nt')) {
$os = 'pc';
}
return $os;
}
/**
* 随机生成字符串
* @param int $length 生成字符的长度
* @param string $chars 指定随机生成的字符串
* @return string
*/
function rand_str($length = 32, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890') {
$chars_length = (strlen($chars) - 1); //Length of character list
$string = $chars{rand(0, $chars_length)}; //Start our string
//Generate random string
for ($i = 1; $i < $length; $i = strlen($string)) {
$r = $chars{rand(0, $chars_length)}; //Grab a random character from our list
if ($r != $string{$i - 1}) {
$string .= $r;
}
//Make sure the same two characters don't appear next to each other
}
return $string; //Return the string
}
/**
* 创建令牌
* @return string
*/
function CreateToken() {
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$chars = str_repeat($chars, 10);
$str = '';
for ($i = 0; $i < 10; $i++) {
$str .= msubstr($chars, floor(mt_rand(0, mb_strlen($chars, 'utf-8') - 1)), 1);
}
$str = md5($str);
session('form_token', $str);
return $str;
}
/**
* 表单令牌验证,防止重复提交
* @param $data
* @return bool
*/
function CheckToken($data) {
if (!empty($data) AND !session('?form_token')) {
# 令牌验证
if ($data === session('form_token')) {
#防止重复提交
session('form_token', null); # 验证完成销毁session
return true;
} else {
return false;
}
}
return TRUE;
}
/**
* 判断是否是微信内置浏览器打开
* @return bool
*/
function is_Weixin() {
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
return true;
}
return false;
}
/**
* 获取参数
* @return bool
*/
function getMapParameter($par, $gets = array()) {
$result = $existArr = array();
if (!empty($par)) {
$par = explode(',', $par);
$i = 0;
foreach ($par as $val) {
//优化重复调用,减少循环
if (array_key_exists($val, $gets)) {
$existArr[$val] = $gets[$val];
continue;
}
$getVal = I('get.' . $val, '', 'htmlspecialchars');
$result[$val] = urldecode($getVal);
$i++;
}
}
$result = array_merge($result, $existArr);
return $result;
}
/**
* CURL获取远程文件大小
* @param type $url
* @param type $user
* @param type $pw
* @return type
*/
function remote_filesize($url, $user = '', $pw = '') {
ob_start();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
if (!empty($user) && !empty($pw)) {
$headers = array('Authorization: Basic ' . base64_encode($user . ':' . $pw));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$okay = curl_exec($ch);
curl_close($ch);
$head = ob_get_contents();
ob_end_clean();
$regex = '/Content-Length:\s([0-9].+?)\s/';
$count = preg_match($regex, $head, $matches);
return isset($matches[1]) ? $matches[1] : FALSE;
}
//替换搜索字符串
function replaceSeachStr($str, $isde = true) {
if ($isde) {
$reStr = str_replace(' ', '\ ', str_replace(')', '\)', str_replace('(', '\(', $str)));
} else {
$reStr = str_replace('\ ', ' ', str_replace('\)', ')', str_replace('\(', '(', $str)));
}
return $reStr;
}
//返回秒随机号
function getMillisecond() {
return (string) time() . (string) mt_rand(10000000, 99999999);
}
//返回微妙的时间差
function microtime_float($millstr) {
if (empty($millstr)) {return false;}
$cle = time() - substr($millstr, 0, 10);
return floor(($cle % (3600 * 24)) / 60);
}
/**
* 模拟get,post,put,delete进行url请求
* @param string $url 请求的URL
* @param string $type 请求类型
* @param string $params 请求参数据
* @param string $headers 请求头
* @return array()
*/
function request_data_type($url, $type, $params = '', $headers = '') {
if (empty($url) || empty($type)) {
return false;
}
$ch = curl_init(); //初始化curl
//$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url); //抓取指定网页
if ($headers != "") {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
} else {
curl_setopt($ch, CURLOPT_HTTPHEADER, 0);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
//curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); //PHP脚本在成功连接服务器前等待多久
switch ($type) {
case "get":
curl_setopt($ch, CURLOPT_HTTPGET, true);
break;
case "post":
curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); //参数
break;
case "put":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); //PUT提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); //参数
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-HTTP-Method-Override: PUT")); //设置HTTP头信息
break;
case "delete":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); //DELETE提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); //参数
break;
}
{
# 设置代理by PHPJungle:
curl_setopt($ch, CURLOPT_PROXY, 'localhost');
curl_setopt($ch, CURLOPT_PROXYPORT, '7777');
}
$data = curl_exec($ch); //获得返回值
curl_close($ch);
return $data;
}
/**
* 递归创建多级目录
* @param type $dirPath
* @param type $mode
* @return boolean
*/
if (!function_exists(mkdirs)) {
function mkdirs($dirPath, $mode = 0777) {
if (!is_dir($dirPath)) {
if (!mkdirs(dirname($dirPath))) {
return FALSE;
}
if (!mkdir($dirPath, $mode)) {
return FALSE;
}
}
return TRUE;
}
}
/**
* 判断目录是否为空
* @param type $dir
* @return boolean
*/
if (!function_exists(isEmptyDir)) {
function isEmptyDir($dir) {
if (!is_dir($dir)) {
return FALSE;
}
$handle = opendir($dir);
if (!$handle) {
return FALSE;
}
$i = 0;
while (($file = readdir($handle)) != FALSE) {
#过滤当前目录和上一级目录
if ($file === '.' || $file === '..') {
continue;
}
$i++;
}
closedir($handle);
if (!$i) {
return true;
} else {
return FALSE;
}
}
}
/**
* 递归删除目录
* @param type $path
* @return boolean
*/
if (!function_exists(deldir)) {
function rmdirs($path) {
//给定的目录不是一个文件夹
if (!is_dir($path)) {
return null;
}
$fh = opendir($path);
while (($row = readdir($fh)) !== false) {
//过滤掉虚拟目录
if ($row == '.' || $row == '..') {
continue;
}
if (!is_dir($path . '/' . $row)) {
unlink($path . '/' . $row);
}
rmdirs($path . '/' . $row);
}
//关闭目录句柄,否则出Permission denied
closedir($fh);
//删除文件之后再删除自身
if (!rmdir($path)) {
echo $path . '无权限删除<br>';
}
return true;
}
}
/* * *************************操作文件 方法 开始************************************************* */
/**
* 建立文件夹
*
* @param string $aimUrl
* @return viod
*/
if (!function_exists(createDir)) {
function createDir($aimUrl) {
$aimUrl = str_replace('', '/', $aimUrl);
$aimDir = '';
$arr = explode('/', $aimUrl);
$result = true;
foreach ($arr as $str) {
$aimDir .= $str . '/';
if (!file_exists($aimDir)) {
$result = mkdir($aimDir);
}
}
return $result;
}
}
/**
* 建立文件
*
* @param string $aimUrl
* @param boolean $overWrite 该参数控制是否覆盖原文件
* @return boolean
*/
if (!function_exists(createFile)) {
function createFile($aimUrl, $overWrite = false) {
if (file_exists($aimUrl) && $overWrite == false) {
return false;
} elseif (file_exists($aimUrl) && $overWrite == true) {
unlinkFile($aimUrl);
}
$aimDir = dirname($aimUrl);
createDir($aimDir);
touch($aimUrl);
return true;
}
}
/**
* 移动文件夹
*
* @param string $oldDir
* @param string $aimDir
* @param boolean $overWrite 该参数控制是否覆盖原文件
* @return boolean
*/
if (!function_exists(moveDir)) {
function moveDir($oldDir, $aimDir, $overWrite = false) {
$aimDir = str_replace('', '/', $aimDir);
$aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/';
$oldDir = str_replace('', '/', $oldDir);
$oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir . '/';
if (!is_dir($oldDir)) {
return false;
}
if (!file_exists($aimDir)) {
createDir($aimDir);
}
@$dirHandle = opendir($oldDir);
if (!$dirHandle) {
return false;
}
while (false !== ($file = readdir($dirHandle))) {
if ($file == '.' || $file == '..') {
continue;
}
if (!is_dir($oldDir . $file)) {
moveFile($oldDir . $file, $aimDir . $file, $overWrite);
} else {
moveDir($oldDir . $file, $aimDir . $file, $overWrite);
}
}
closedir($dirHandle);
return rmdir($oldDir);
}
}
/**
* 移动文件
*
* @param string $fileUrl
* @param string $aimUrl
* @param boolean $overWrite 该参数控制是否覆盖原文件
* @return boolean
*/
if (!function_exists(moveFile)) {
function moveFile($fileUrl, $aimUrl, $overWrite = false) {
if (!file_exists($fileUrl)) {
return false;
}
if (file_exists($aimUrl) && $overWrite = false) {
return false;
} elseif (file_exists($aimUrl) && $overWrite = true) {
unlinkFile($aimUrl);
}
$aimDir = dirname($aimUrl);
createDir($aimDir);
rename($fileUrl, $aimUrl);
return true;
}
}
/**
* 删除文件夹
*
* @param string $aimDir
* @return boolean
*/
if (!function_exists(unlinkDir)) {
function unlinkDir($aimDir) {
$aimDir = str_replace('', '/', $aimDir);
$aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/';
if (!is_dir($aimDir)) {
return false;
}
$dirHandle = opendir($aimDir);
while (false !== ($file = readdir($dirHandle))) {
if ($file == '.' || $file == '..') {
continue;
}
if (!is_dir($aimDir . $file)) {
unlinkFile($aimDir . $file);
} else {
unlinkDir($aimDir . $file);
}
}
closedir($dirHandle);
return rmdir($aimDir);
}
}
/**
* 删除文件
*
* @param string $aimUrl
* @return boolean
*/
if (!function_exists(unlinkFile)) {
function unlinkFile($aimUrl) {
if (file_exists($aimUrl)) {
unlink($aimUrl);
return true;
} else {
return false;
}
}
}
/**
* 复制文件夹
*
* @param string $oldDir
* @param string $aimDir
* @param boolean $overWrite 该参数控制是否覆盖原文件
* @return boolean
*/
if (!function_exists(copyDir)) {
function copyDir($oldDir, $aimDir, $overWrite = false) {
$aimDir = str_replace('', '/', $aimDir);
$aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/';
$oldDir = str_replace('', '/', $oldDir);
$oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir . '/';
if (!is_dir($oldDir)) {
return false;
}
if (!file_exists($aimDir)) {
createDir($aimDir);
}
$dirHandle = opendir($oldDir);
while (false !== ($file = readdir($dirHandle))) {
if ($file == '.' || $file == '..') {
continue;
}
if (!is_dir($oldDir . $file)) {
copyFile($oldDir . $file, $aimDir . $file, $overWrite);
} else {
copyDir($oldDir . $file, $aimDir . $file, $overWrite);
}
}
return closedir($dirHandle);
}
}
function getFilePathstr($fineName) {
if (empty($fineName)) {
return '';
}
$v1 = substr($fineName, 0, 2);
$v2 = substr($fineName, 2, 2);
return $v1 . '/' . $v2 . '/';
}
/**
* 复制文件
*
* @param string $fileUrl
* @param string $aimUrl
* @param boolean $overWrite 该参数控制是否覆盖原文件
* @return boolean
*/
if (!function_exists(copyFile)) {
function copyFile($fileUrl, $aimUrl, $overWrite = false) {
if (!file_exists($fileUrl)) {
return false;
}
if (file_exists($aimUrl) && $overWrite == false) {
return false;
} elseif (file_exists($aimUrl) && $overWrite == true) {
unlinkFile($aimUrl);
}
$aimDir = dirname($aimUrl);
createDir($aimDir);
copy($fileUrl, $aimUrl);
return true;
}
}
/**
* 格式化输出到文件
*/
function fdump($arr) {
ob_start();
echo "\r\n";
array_walk(func_get_args(), create_function('&$item, $key', 'print_r($item);'));
echo "\r\n";
file_put_contents('../debug.txt', ob_get_contents(), FILE_APPEND);
ob_end_clean();
}
/** *************************操作文件 方法 结束************************************************* */
function remove_script($v) {
if (strpos($v, 'script') !== false || strpos($v, '"') !== false || strpos($v, '\'') !== false || strpos($v, '>') !== false || strpos($v, '<') !== false) {
$v = '';
}
return $v;
}没有难的技术,当你弄清它的原理时,你会发现原来如此简单~ 欢迎加群【195474288】讨论
