php上传功能集后缀名判断和随机命名(强力推荐)

本篇文章给大家分享php上传功能集后缀名判断和随机命名,代码写的简单易懂,感兴趣的朋友快来参考下吧。

不废话了,具体请看下文代码示例讲解。

form.php

  1. <html>
  2. <head>
  3. <meta http-equiv="content-type" content="text/html" charset="utf-8">
  4. <title>Upload Image</title>
  5. </head>
  6. <body>
  7. <form method="post" action="upload.php" enctype="multipart/form-data">
  8. <input type="hidden" name="MAX_FILE_SEZE" value="2000000">
  9. <input type="file" name="file" value="view">
  10. <input type="submit" value="upload" name="B1">
  11. </form>
  12. </body>
  13. </html>

upload.php

  1. <?php
  2. include("check.php"); // 引入自定义函数文件
  3. $type = array("jpg", "gif", "bmp", "jpeg", "png");
  4. // 判断上传文件类型
  5. $fileext = strtolower(fileext($_FILES['file']['name']));
  6. $uploadfilename = random(8);
  7. if(in_array($fileext, $type)){
  8. $filename = explode(".", $_FILES['file']['name']);
  9. if(is_uploaded_file($_FILES['file']['tmp_name'])){
  10. // echo $_FILES['file']['tmp_name'];
  11. $flag = move_uploaded_file($_FILES['file']['tmp_name'], "/Library/WebServer/Documents/test/".$uploadfilename.".".$fileext);
  12. if($flag){
  13. echo "上传成功!";
  14. }else{
  15. echo "Error.";
  16. }
  17. echo "<a href='javascript:history.go(-1)'>Back</a>";
  18. }
  19. }

check.php

  1. <?php
  2. header("Content-type:text/html;charset=utf8");
  3. // 获取文件后缀名函数
  4. function fileext($filename){
  5. $sTemp = strrchr($filename, ".");
  6. return substr($sTemp, 1);
  7. }
  8. function fileext2($filename){
  9. $sTemp = explode(".", $filename);
  10. return $sTemp[count($sTemp)-1];
  11. }
  12. // 生成随机文件名函数
  13. function random($length){
  14. $captchaSource = "0123456789abcdefghijklmnopqrstuvwxyz这是一个随机打印输出字符串的例子";
  15. $captchaResult = "2015"; // 随机数返回值
  16. $captchaSentry = ""; // 随机数中间变量
  17. for($i=0;$i<$length;$i++){
  18. $n = rand(0, 35); #strlen($captchaSource));
  19. if($n >= 36){
  20. $n = 36 + ceil(($n-36)/3) * 3;
  21. $captchaResult .= substr($captchaSource, $n, 3);
  22. }else{
  23. $captchaResult .= substr($captchaSource, $n, 1);
  24. }
  25. }
  26. return $captchaResult;
  27. }
  28. ?>

将三个文件整合成一个:

  1. <?php
  2. // 获取文件后缀名函数
  3. function fileext($filename){
  4. $sTemp = strrchr($filename, ".");
  5. return substr($sTemp, 1);
  6. }
  7. function fileext2($filename){
  8. $sTemp = explode(".", $filename);
  9. return $sTemp[count($sTemp)-1];
  10. }
  11. // 生成随机文件名函数
  12. function random($length){
  13. $captchaSource = "0123456789abcdefghijklmnopqrstuvwxyz这是一个随机打印输出字符串的例子";
  14. $captchaResult = "2015"; // 随机数返回值
  15. $captchaSentry = ""; // 随机数中间变量
  16. for($i=0;$i<$length;$i++){
  17. $n = rand(0, 35); #strlen($captchaSource));
  18. if($n >= 36){
  19. $n = 36 + ceil(($n-36)/3) * 3;
  20. $captchaResult .= substr($captchaSource, $n, 3);
  21. }else{
  22. $captchaResult .= substr($captchaSource, $n, 1);
  23. }
  24. }
  25. return $captchaResult;
  26. }
  27. $type = array("jpg", "gif", "bmp", "jpeg", "png");
  28. // 判断上传文件类型
  29. $fileext = strtolower(fileext($_FILES['file']['name']));
  30. $uploadfilename = random(8);
  31. if(in_array($fileext, $type)){
  32. $filename = explode(".", $_FILES['file']['name']);
  33. if(is_uploaded_file($_FILES['file']['tmp_name'])){
  34. // echo $_FILES['file']['tmp_name'];
  35. $flag = move_uploaded_file($_FILES['file']['tmp_name'], "/Library/WebServer/Documents/test/".$uploadfilename.".".$fileext);
  36. if($flag){
  37. echo "上传成功!";
  38. }else{
  39. echo "Error.";
  40. }
  41. echo "<a href='javascript:history.go(-1)'>Back</a>";
  42. }
  43. }
  44. ?>
  45. <html>
  46. <head>
  47. <meta http-equiv="content-type" content="text/html" charset="utf-8">
  48. <title>Upload Image</title>
  49. </head>
  50. <body>
  51. <form method="post" action="" enctype="multipart/form-data">
  52. <input type="hidden" name="MAX_FILE_SEZE" value="2000000">
  53. <input type="file" name="file" value="view">
  54. <input type="submit" value="upload" name="B1">
  55. </form>
  56. </body>
  57. </html>

以上内容就是给大家讲解的php上传功能集后缀名判断和随机命名(强力推荐),希望大家喜欢。