Correction status:qualified
Teacher's comments:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" enctype="multipart/form-data"> <!-- 用隐藏域设置允许上传的文件大小,仅考参考 --> <input type="hidden" name="MAX_FILE_SIZE" value="542488"> <fieldset> <legend align="center"> 文件上传 </legend> <p> <strong>选择文件:</strong> <input type="file" name="upload"> </p> </fieldset> <p align="center"> <button type="submit" name="submit" > 上传 </button> </p> </form> <?php if ($_SERVER['REQUEST_METHOD'] == 'POST'){ echo '判断请求类型为post成功<br>'; if (isset($_FILES['upload'])){ echo '判断控件数据成功<br>'; $allow = ['image/jpg','image/jpeg','image/png',]; if (in_array($_FILES['upload']['type'], $allow)){ echo '判断文件为图片成功<br>'; if (move_uploaded_file($_FILES['upload']['tmp_name'], "upload/{$_FILES['upload']['name']}")){ echo '判断文件上传成功<br>文件名为'; echo $_FILES['upload']['name']; } }else{ echo '文件非图片类型上传失败<br>'; } } if ($_FILES['upload']['error'] > 0 ){ echo '错误原因'; switch ($_FILES['upload']['error']) { case 1: echo '文件超过了php.ini配置中设置的大小'; break; case 2: echo '文件超过了表单中常量设置的大小'; break; case 3: echo '仅有部分文件被上传'; break; case 4: echo '没有文件被上传'; break; case 6: echo '没有可用的临时文件夹'; break; case 7: echo '磁盘已满,写入失败'; break; case 8: echo '上传意外中止'; break; default: echo '系统未知错误'; break; } if (file_exists($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name'])) { unlink($_FILES['upload']['tmp_name']); } } } ?>
对每一步判断进行输出检查,更加容易检查错误和修改