PHP上传文件
直接附上代码吧!
先看上传单个文件
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>上传文件</title> </head> <body> <!-- 上传文件必须将method改为post,并且加enctype="multipart/form-data" --> <form action="" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="10000000"> <input type="file" name="file"> <input type="submit" name="submit" value="upload"> </form> <br><br><br><br> </body> </html> <?php if(isset($_POST['submit'])){ $flag=true; //验证文件大小 if($_FILES['file']['size']>$_POST['MAX_FILE_SIZE']){ echo "文件".$_FILES['file']['name']."的大小超出范围<br>"; $flag=false; continue; } else { echo "文件".$_FILES['file']['name']."的符合大小要求<br>"; } echo $_FILES['file']['type']; //验证文件格式 $new_name="./upload/";//上传文件的根目录 switch($_FILES['file']['type']){ case "image/png": case "image/gif": case "image/jpeg": case "image/x-png": echo "上传的文件类型为图片<br>"; $new_name.="pic/"; break; case "text/txt": case "text/plain": case "text/html": echo "上传的文件类型为文本<br>"; $new_name.="text/"; break; case "audio/basic": case "audio/x-m4a": echo "上传的文件类型为音频<br>"; $new_name.="audio/"; break; case "video/mpeg": echo "上传的文件类型为音频<br>"; $new_name.="video/"; break; case "application/zip": case "application/x-tar": case "application/x-gz": $new_name.="rar/"; echo "上传的文件类型为压缩文件<br>"; break; case "application/msword": case "application/powerpoint": case "application/vnd.ms-excel": $new_name.="office/"; echo "上传的文件类型为office办公文档<br>"; break; case "application/x-msdownload": $new_name.="software/"; echo "上传的文件类型是可执行文件<br>"; break; default: echo "文件类型不合格<br>"; $flag=false; break; } //上传的文件名,可采用原文件名,也可采用时间加随机数作文件名 // $new_name.=$_FILES['file']['name'][$i]; $tmp_name=$_FILES['file']['name']; $arr=explode(".",$tmp_name); $extension=array_pop($arr); //一定要注意采集文件后缀名时,有这种类型abc.1.5.exe,即有版本号的情况 $new_name.=date("YmdHis").rand(0,99).".".$extension; if($flag&&move_uploaded_file($_FILES['file']['tmp_name'],$new_name)){ echo "上传成功<br><br><br><br>"; } else { echo "上传失败<br><br><br><br>"; } } ?>
再看上传多个文件
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>上传文件</title> </head> <body> <!-- 上传文件必须将method改为post,并且加enctype="multipart/form-data" --> <form action="" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="10000000"> <input type="file" name="file[]"><br> <input type="file" name="file[]"><br> <input type="file" name="file[]"><br> <input type="file" name="file[]"><br><br> <input type="submit" name="submit" value="upload"><br> </form> <br><br><br><br> </body> </html> <?php if(isset($_POST['submit'])){ //获取上传的文件数 $total_file_nums=count($_FILES['file']['name']); for($i=0;$i<$total_file_nums;$i++){ $flag=true; //验证文件大小 if($_FILES['file']['size'][$i]>$_POST['MAX_FILE_SIZE']){ echo "文件".$_FILES['file']['name'][$i]."的大小超出范围<br>"; $flag=false; continue; } else { echo "文件".$_FILES['file']['name'][$i]."的符合大小要求<br>"; } echo $_FILES['file']['type'][$i]; //验证文件格式 $new_name="./upload/";//上传文件的根目录 switch($_FILES['file']['type'][$i]){ case "image/png": case "image/gif": case "image/jpeg": case "image/x-png": echo "上传的文件类型为图片<br>"; $new_name.="pic/"; break; case "text/txt": case "text/plain": case "text/html": echo "上传的文件类型为文本<br>"; $new_name.="text/"; break; case "audio/basic": case "audio/x-m4a": echo "上传的文件类型为音频<br>"; $new_name.="audio/"; break; case "video/mpeg": echo "上传的文件类型为音频<br>"; $new_name.="video/"; break; case "application/zip": case "application/x-tar": case "application/x-gz": $new_name.="rar/"; echo "上传的文件类型为压缩文件<br>"; break; case "application/msword": case "application/powerpoint": case "application/vnd.ms-excel": $new_name.="office/"; echo "上传的文件类型为office办公文档<br>"; break; case "application/x-msdownload": $new_name.="software/"; echo "上传的文件类型是可执行文件<br>"; break; default: echo "文件类型不合格<br>"; $flag=false; break; } //上传的文件名,可采用原文件名,也可采用时间加随机数作文件名 // $new_name.=$_FILES['file']['name'][$i]; $tmp_name=$_FILES['file']['name'][$i]; $arr=explode(".",$tmp_name); $extension=array_pop($arr); //一定要注意采集文件后缀名时,有这种类型abc.1.5.exe,即有版本号的情况 $new_name.=date("YmdHis").rand(0,99).".".$extension; if($flag&&move_uploaded_file($_FILES['file']['tmp_name'][$i],$new_name)){ echo "上传成功<br><br><br><br>"; } else { echo "上传失败<br><br><br><br>"; } } } ?>
其实这个代码上传多个文件有很多bug,比如只上传一个,或者个数不确定,都不好用
如需转载,请注明文章出处,谢谢!!!