这篇文章主要介绍“php如何实现上传功能集后缀名判断和随机命名”,在日常操作中,相信很多人在php如何实现上传功能集后缀名判断和随机命名问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”php如何实现上传功能集后缀名判断和随机命名”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
不废话了,具体请看下文代码示例讲解。
form.php
<html> <head> <meta http-equiv="content-type" content="text/html" charset="utf-8"> <title>Upload Image</title> </head> <body> <form method="post" action="upload.php" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SEZE" value="2000000"> <input type="file" name="file" value="view"> <input type="submit" value="upload" name="B1"> </form> </body> </html>
upload.php
<?php include("check.php"); // 引入自定义函数文件 $type = array("jpg", "gif", "bmp", "jpeg", "png"); // 判断上传文件类型 $fileext = strtolower(fileext($_FILES['file']['name'])); $uploadfilename = random(8); if(in_array($fileext, $type)){ $filename = explode(".", $_FILES['file']['name']); if(is_uploaded_file($_FILES['file']['tmp_name'])){ // echo $_FILES['file']['tmp_name']; $flag = move_uploaded_file($_FILES['file']['tmp_name'], "/Library/WebServer/Documents/test/".$uploadfilename.".".$fileext); if($flag){ echo "上传成功!"; }else{ echo "Error."; } echo "<a href='javascript:history.go(-1)'>Back</a>"; } }
check.php
<?php header("Content-type:text/html;charset=utf8"); // 获取文件后缀名函数 function fileext($filename){ $sTemp = strrchr($filename, "."); return substr($sTemp, 1); } function fileext2($filename){ $sTemp = explode(".", $filename); return $sTemp[count($sTemp)-1]; } // 生成随机文件名函数 function random($length){ $captchaSource = "0123456789abcdefghijklmnopqrstuvwxyz这是一个随机打印输出字符串的例子"; $captchaResult = "2015"; // 随机数返回值 $captchaSentry = ""; // 随机数中间变量 for($i=0;$i<$length;$i++){ $n = rand(0, 35); #strlen($captchaSource)); if($n >= 36){ $n = 36 + ceil(($n-36)/3) * 3; $captchaResult .= substr($captchaSource, $n, 3); }else{ $captchaResult .= substr($captchaSource, $n, 1); } } return $captchaResult; } ?>
将三个文件整合成一个:
<?php // 获取文件后缀名函数 function fileext($filename){ $sTemp = strrchr($filename, "."); return substr($sTemp, 1); } function fileext2($filename){ $sTemp = explode(".", $filename); return $sTemp[count($sTemp)-1]; } // 生成随机文件名函数 function random($length){ $captchaSource = "0123456789abcdefghijklmnopqrstuvwxyz这是一个随机打印输出字符串的例子"; $captchaResult = "2015"; // 随机数返回值 $captchaSentry = ""; // 随机数中间变量 for($i=0;$i<$length;$i++){ $n = rand(0, 35); #strlen($captchaSource)); if($n >= 36){ $n = 36 + ceil(($n-36)/3) * 3; $captchaResult .= substr($captchaSource, $n, 3); }else{ $captchaResult .= substr($captchaSource, $n, 1); } } return $captchaResult; } $type = array("jpg", "gif", "bmp", "jpeg", "png"); // 判断上传文件类型 $fileext = strtolower(fileext($_FILES['file']['name'])); $uploadfilename = random(8); if(in_array($fileext, $type)){ $filename = explode(".", $_FILES['file']['name']); if(is_uploaded_file($_FILES['file']['tmp_name'])){ // echo $_FILES['file']['tmp_name']; $flag = move_uploaded_file($_FILES['file']['tmp_name'], "/Library/WebServer/Documents/test/".$uploadfilename.".".$fileext); if($flag){ echo "上传成功!"; }else{ echo "Error."; } echo "<a href='javascript:history.go(-1)'>Back</a>"; } } ?> <html> <head> <meta http-equiv="content-type" content="text/html" charset="utf-8"> <title>Upload Image</title> </head> <body> <form method="post" action="" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SEZE" value="2000000"> <input type="file" name="file" value="view"> <input type="submit" value="upload" name="B1"> </form> </body> </html>
到此,关于“php如何实现上传功能集后缀名判断和随机命名”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。