1. 返回文件扩展名
1 function getformat($file)
2 {
3 $ext=strrchr($file,".");
4 $format=strtolower($ext);
5 return $format;
6 }
2 {
3 $ext=strrchr($file,".");
4 $format=strtolower($ext);
5 return $format;
6 }
2.格式化变量
1 <?
2 $num = 1;
3 printf("%04d", $num);
4 ?>
2 $num = 1;
3 printf("%04d", $num);
4 ?>
3.php重定向网页
1 // 例如重定向到www.cgsir.com (注意重定向之前不要有html内容)
2 header("location:http://www.jb51.net");
3 或
4 echo "<meta http-equiv='refresh' content='0;url=http://www.jb51.net.com'>";
2 header("location:http://www.jb51.net");
3 或
4 echo "<meta http-equiv='refresh' content='0;url=http://www.jb51.net.com'>";
4.限制上传的文件大小
1 //$limit_size为限制最大文件大小
2 $limit_size=50000;
3 $file_size=$HTTP_POST_FILES['ufile']['size'];
4 if($file_size >= $limit_size) {
5 echo "你的文件超过的限制的大小<BR>";
6 echo "你的文件大小为= ".$file_size;
7 echo " K";
8 echo "<BR>文件大小限制为= 50000 k";
9 }
10 else {
11 // 上传到什么目录,也就是从临时目录拷贝到目标目录
12 if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
13 {
14 echo "上传成功<BR/>";
15 echo "<img src=\"$path\" width=\"150\" height=\"150\">";
16 }
2 $limit_size=50000;
3 $file_size=$HTTP_POST_FILES['ufile']['size'];
4 if($file_size >= $limit_size) {
5 echo "你的文件超过的限制的大小<BR>";
6 echo "你的文件大小为= ".$file_size;
7 echo " K";
8 echo "<BR>文件大小限制为= 50000 k";
9 }
10 else {
11 // 上传到什么目录,也就是从临时目录拷贝到目标目录
12 if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
13 {
14 echo "上传成功<BR/>";
15 echo "<img src=\"$path\" width=\"150\" height=\"150\">";
16 }
5.php常用的对字符串进行加密的算法:
5.1 $db_password = md5($random_password);
5.2 $db_password = sh1($random_password);
6.退出登录
1 // 退出登录
2 session_start();
3 include_once('includes/header.php');
4 if (isset($_SESSION['user_id']))
5 {
6 unset($_SESSION['user_id']);
7 session_destroy();
8 echo '<div align="center">';
9 echo '<span class="STYLE1">成功退出!</span><br />';
10 echo '<p><span class="STYLE1">正在跳转,请稍等</span></p>';
11 echo '<script language="javascript">';
12 echo 'function Jump()';
13 echo '{ ';
14 echo ' parent.location.href="index.php" ';
15 echo '} ' ;
16 echo 'document.onload = setTimeout("Jump()" , 2 * 1000)';
17 echo '</script>';
18 echo '<span class="STYLE1"><a href="index.php">直接返回</a></span><br /><br />';
19 echo '</div>';
20 exit(0);
21 }
22 else
23 {
24 echo '<span class="STYLE1">您还没有登录呢!</span>';
25 }
26 include_once('includes/footer.php');
27 ?>
28
2 session_start();
3 include_once('includes/header.php');
4 if (isset($_SESSION['user_id']))
5 {
6 unset($_SESSION['user_id']);
7 session_destroy();
8 echo '<div align="center">';
9 echo '<span class="STYLE1">成功退出!</span><br />';
10 echo '<p><span class="STYLE1">正在跳转,请稍等</span></p>';
11 echo '<script language="javascript">';
12 echo 'function Jump()';
13 echo '{ ';
14 echo ' parent.location.href="index.php" ';
15 echo '} ' ;
16 echo 'document.onload = setTimeout("Jump()" , 2 * 1000)';
17 echo '</script>';
18 echo '<span class="STYLE1"><a href="index.php">直接返回</a></span><br /><br />';
19 echo '</div>';
20 exit(0);
21 }
22 else
23 {
24 echo '<span class="STYLE1">您还没有登录呢!</span>';
25 }
26 include_once('includes/footer.php');
27 ?>
28
7.截取中文字符串函数
1 function cut_substring($str,$cat_len, $start = null) {
2 $result = "";
3
4 if ( $start < 0 ) $start = 0;
5 if ( $start == "" || $start == null ) {
6 if ( $cat_len == "" || $cat_len == null ) {
7 return false;
8 } elseif ( $cat_len > strlen($str) ) {
9 $result = $str;
10 } else {
11 for ( $i = 0; $i < $cat_len;) {
12 if ( ord($str{$i}) > 127 ) {
13 if ( $i + 3 <= $cat_len ) $result .= substr($str, $i, 3);
14 $i += 3;
15 } else {
16 $result .= substr($str, $i, 1);
17 $i += 1;
18 }
19 }
20 }
21 return $result;
22 } elseif ( $start >= strlen($str) ) {
23 return false;
24 } else {
25 $result1 = cut_substring($str, $start);
26 $result1_len = strlen($result1);
27 if ( $result1_len != $start ) $result1_len = $result1_len + 3;
28 $str = substr($str , $result1_len);
29 $result = cut_substring($str, $cat_len);
30 }
31 return $result;
32 }
33
34
35
2 $result = "";
3
4 if ( $start < 0 ) $start = 0;
5 if ( $start == "" || $start == null ) {
6 if ( $cat_len == "" || $cat_len == null ) {
7 return false;
8 } elseif ( $cat_len > strlen($str) ) {
9 $result = $str;
10 } else {
11 for ( $i = 0; $i < $cat_len;) {
12 if ( ord($str{$i}) > 127 ) {
13 if ( $i + 3 <= $cat_len ) $result .= substr($str, $i, 3);
14 $i += 3;
15 } else {
16 $result .= substr($str, $i, 1);
17 $i += 1;
18 }
19 }
20 }
21 return $result;
22 } elseif ( $start >= strlen($str) ) {
23 return false;
24 } else {
25 $result1 = cut_substring($str, $start);
26 $result1_len = strlen($result1);
27 if ( $result1_len != $start ) $result1_len = $result1_len + 3;
28 $str = substr($str , $result1_len);
29 $result = cut_substring($str, $cat_len);
30 }
31 return $result;
32 }
33
34
35
8.高效获取URL中的文件扩展名
1 function($url) {
2
3 $w_param = pathinfo($url);
4
5 $str = $w_param['extension'];
6
7 list($type, $vars) = explode('?',$str);
8
9 return $type;
10 }
11
2
3 $w_param = pathinfo($url);
4
5 $str = $w_param['extension'];
6
7 list($type, $vars) = explode('?',$str);
8
9 return $type;
10 }
11