常用函数

  • 生成 n 位数的随机验证码

    function random($length = 6 , $numeric = 0) { PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000); if($numeric) { $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1)); } else { $hash = ''; $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz'; $max

  • 递归循环某目录下的子目录及文件并输出

    function tree($dir,$level=1){ $fh=opendir($dir);//打开目录 while($dirname=readdir($fh)){//循环读取当前目录下的子目录及文件 if($dirname == '.' || $dirname == '..'){ continue; } echo "|".str_repeat('-',$level).$dirname;//目录名或文件名,str_repeat("|-",$num);//把字符串重复指定的次数 echo '<br>'; if(is_dir($dir. '/' .$dirname)){//如果是目

  • php记录日志信息到文件中

    // 在测试代码时,需要了解代码执行情况,而这中执行时在后台运行的,前台无法知道是否运行正常,在这种情况下,我们一般用写日志的形式来调试代码。 function logResult($str='') { $fp = fopen("log.txt","a"); flock($fp, LOCK_EX) ; fwrite($fp,"执行日期:".strftime("%Y%m%d%H%M%S",time())."\n".$str."\n"); flock($fp, LOCK_UN); fclose($fp); } //函数logResult()记录执行时间,参数$str自定义,执行时会将运行日志写入到

  • php强制下载文件

    //有时我们不想让浏览器直接打开文件,如PDF文件,而是要直接下载文件,那么以下函数可以强制下载文件,函数中使用了application/octet-stream头类型 function download($filename) { if ((isset($filename))&&(file_exists($filename))){ header("Content-length: ".filesize($filename)); header('Content-Type: application/octet-stream'); header('Content-Disposition

  • php获取当前页面的完整url

    PHP获取当前页面的完整url,如:"http://www.pengxb.com/article/23"//以下函数可以获取当前页面的URL,不管是http还是https function curPageURL() { $pageURL = 'http'; if (!empty($_SERVER['HTTPS'])) {$pageURL .= "s";} $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"]

  • curl请求函数

    1.封装请求函数if(!function_exists('curl_request')){ //发送curl请求 function curl_request($url, $type = false, $params = [], $https=false) { //调用curl_init() 初始化请求 $ch = curl_init($url); //调用curl_setopt()设置请求选项 if($type){ //true 发送post请求 false 默认发送get请求 //post请求 设置请求方式 curl_setopt($ch, CURLOPT_POST, true); //设置