批改状态:合格
老师批语:
<?php// printf('<pre>%s</pre>',print_r($_FILES,true));// 遍历:有多少个文件上传$imgs = [];foreach ($_FILES as $file) {printf('<pre>%s</pre>',print_r($_FILES,true));if($file['error']==0){if(strstr($file['type'],'/',true)!=='image'){$tips = '文件类型错误';// echo $tips;continue;}else{//创建目标文件名,只要保证不重名 $targetName='uploads/'.md5($file['name']).strstr($file['name'],'.'); if(!move_uploaded_file($file['tmp_name'],$targetName)){$tips = '文件上传失败';}else{$imgs[]=$targetName;}}}}?>

二).多个文件上传
<?php// printf('<pre>%s</pre>',print_r($_FILES,true));// 遍历:有多少个文件上传$imgs = [];if(!isset($_FILES['my_pic'])){$tips='没有文件上传';}else{foreach ($_FILES['my_pic']['error'] as $key=>$error) {if($error==0){$file =$_FILES['my_pic'];if(strstr($file['type'][$key],'/',true)!=='image'){$tips = '文件类型错误';// echo $tips;continue;}else{//创建目标文件名,只要保证不重名$targetName='uploads/'.md5($file['name'][$key]).strstr($file['name'][$key],'.');if(!move_uploaded_file($file['tmp_name'][$key],$targetName)){$tips = '文件上传失败';}else{$imgs[]=$targetName;}}}elseif($error==1){}}}?><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>文件上传</title></head><body><!-- 1.action:空,表示提交到当前的页面2.method:post3.enctype="multipart/form-data"--><form action="" method="post" enctype="multipart/form-data" ><fieldset><legend>文件批量上传</legend><input type="file" name="my_pic[]" multiple><button>上传</button></fieldset></form><?php if(isset($tips)): ?><?=$tips?><?php elseif(count($imgs)>0) : ?><?php foreach($imgs as $img) : ?><img src="<?=$img?>" width="200" /><?php endforeach;?><?php endif ?></body></html>

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号