<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>多文件上传</title> </head> <body> <form action="uploads.php" method="post" enctype="multipart/form-data"> <input type="file" name="pic[]" /><br /> <input type="file" name="pic[]" /><br /> <input type="file" name="pic[]" /><br /> <input type="file" name="pic[]" /><br /> <input type="file" name="pic[]" /><br /> <input type="submit" value="提交" /> </form> </body> </html> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>多文件上传</title> </head> <body> <form action="uploads.php" method="post" enctype="multipart/form-data"> <input type="file" name="pic[]" multiple /><br /> <input type="submit" name="sub" value="提交" /> </form> </body> </html>
<?php $file = $_FILES; $num = count($file[‘pic‘][‘name‘]); //html5一次性多文件上传,固定数量和非固定数量 for($i = 0; $i < $num; $i++){ if(is_uploaded_file($file[‘pic‘][‘tmp_name‘][$i])){ $allowType = array("iamge/png", "image/jpeg", "image/jpg", "iamge/gif"); $type = $file[‘pic‘][‘type‘][$i]; if(!in_array($type, $allowType)){ echo "不允许的类型<br />"; continue; } $path = "./uploads/"; $fname = date(‘YmdHis‘).rand(1000,9999); $ext = array_pop(explode(‘.‘, $file[‘pic‘][‘name‘][$i])); $newFile = $path.$fname.‘.‘.$ext; if(move_uploaded_file($file[‘pic‘][‘tmp_name‘][$i], $newFile)){ $n = $i + 1; echo "上传第{$n}张图片成功<br />"; }else{ $n = $i + 1; echo "上传第{$n}张图片失败<br />"; } } }
时间: 2024-11-04 06:42:14