#删除不为空的目录
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
}
}
reset($objects);
rmdir($dir);
}
}
#批量创建目录
function MakeDirectory($dir, $mode = 0755)
{
umask(0);
if (is_dir($dir) || @mkdir($dir,$mode)) return TRUE;
if (!MakeDirectory(dirname($dir),$mode)) return FALSE;
return @mkdir($dir,$mode);
}
#批量创建目录
function recursive_mkdir($path, $mode = 0777) {
$dirs = explode(DIRECTORY_SEPARATOR , $path);
$count = count($dirs);
$path = substr($path, 0, 1) === DIRECTORY_SEPARATOR ? DIRECTORY_SEPARATOR : '.';
umask(0);
for ($i = 0; $i < $count; ++$i) {
$path .= DIRECTORY_SEPARATOR . $dirs[$i];
if (!is_dir($path) && !mkdir($path, $mode)) {
return false;
}
}
return true;
}
/**
* 多进程同时写入一个文件 不支持NFS文件系统
* @param string $filename
* @param string $str
*/
function write($filename, $str) {
$f = fopen ( $filename, "a" );
if (flock ( $f, LOCK_EX )) {
fwrite ( $f, $str );
//sleep(5);
flock ( $f, LOCK_UN );
}
fclose ( $f );
}
write("/home/v_jksong/lock.data", "sjk");
/**
* 判断是否是合法整形 加强版
* @param unknown_type $a
* @return boolean
*/
function is_intval($a) {
return ((string)$a === (string)(int)$a);
}
// true for ("123", "0", "-1", 0, 11, 9011, 00, 0x12, true)
// false for (" ", "", 1.1, "123.1", "00", "0x123", "123a", "ada", "--1", "999999999999999999999999999999999", false, null, '1 ')
/**
* 检测一维数组是否相等,如果(string)$v1 === (string)$v2 则为相等
* @param unknown_type $a
* @param unknown_type $b
* @param unknown_type $assoc
* @return boolean
*/
function array_equal($a, $b, $assoc = true)
{
$func = 'array_diff';
if($assoc === true)
{
$func = 'array_diff_assoc';
}
$diff_a_b = $func($a, $b);
$diff_b_a = $func($b, $a);
if(empty($diff_a_b) && empty($diff_b_a))
{
return true;
}
return false;
}
//解析 js中escap编码的字符串
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;
}
echo js_unescape('%u4E03%u5915%u628A%u59B9%u5FC5%u5907%20%u56DB%u6B3ESUV%u6700%u9AD8%u964D6.5%u4E07_%u5927%u7CA4%u7F51_%u817E%u8BAF%u7F51');
/**
* 获取ip地址,注意服务器是双网卡的情况
* @param unknown_type $dest
* @param unknown_type $port
* @return unknown
*/
function my_ip($dest='64.0.0.0', $port=80)
{
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_connect($socket, $dest, $port);
socket_getsockname($socket, $addr, $port);
socket_close($socket);
return $addr;
}
/**
* 用以取代file_get_contents函数,可以设置主机名以及超时时间等设置
* @param $url 主机名
* @param $path 路径
* @param $port=80 端口
* @param $timeout=10 超时设置,单位秒
* @return integer/string -1:连接不上主机 -2:发送请求串失败
*/
function get_file_contents($url,$path,$port=80,$timeout=10)
{
$errno=null;
$errstr=null;
$fp=fsockopen($url,$port);
if(!$fp)
{
return -1;
}
$command='GET '.$path.' HTTP/1.1'."
";
$command.='Accept: */*'."
";
$command.='Accept-Language: zh-cn'."
";
$command.='Host: '.$url."
";
$command.='Connection: Close'."
";
$command.="
";
$flag=fwrite($fp,$command);
if(!$flag)
{
fclose($fp);
return -2;
}
stream_set_timeout($fp,$timeout);
$tmp_ret_str="";
$flag=false;
while(!feof($fp))
{
$tmp_cur_line=fgets($fp,2048);
if($flag)
{
$tmp_ret_str.=$tmp_cur_line;
}
if($tmp_cur_line=="
")
{
$flag=true;
}
}
fclose($fp);
return $tmp_ret_str;
}
/**
* URL取得相应数据的函数
*
* @param string $url 完整URL
* @param int $timeout 超时时间
* @return string/errorno
*/
function get_url_contents($url,$timeout=10)
{
$command='wget --timeout '.$timeout.' -q -O- "'.$url.'"';
$tmp_ret_str='';
$fp=popen($command,"r");
if($fp==false)
{
return -1;
}
while(!feof($fp))
{
$tmp_ret_str.=fgets($fp);
}
pclose($fp);
return $tmp_ret_str;
}
/**
* 获取当前内存情况
* @return string
*/
function memory_usage($isEcho = 0)
{
$memUsage = memory_get_usage();
$unitArr = array("B", "K", "M", "G");
$used = @round($memUsage/pow(1024, ($i=floor(log($memUsage, 1024)))), 2)."".($unitArr[$i]);
if($isEcho)
{
ee($used);
}
return $used;
}