批改状态:合格
老师批语:文件上传有许多要注意的地方
代码部分:
<?phpclass upException extends Exception{public function __toString(){return <<<UPE<table><tr><td>错误编号</td><td>错误信息</td><td>错误文件</td><td>错误行号</td></tr><tr><td>$this->code</td><td>$this->message</td><td>$this->file</td><td>$this->line</td></tr></table><style>table{text-align: center;border: 2px solid #000;border-collapse: collapse;}td { border:1px solid black;padding: 10px;}tr:first-of-type {background-color:#eee;}tr:last-of-type td {color:lightsalmon;background-color:lightskyblue;}</style>UPE;}}try{//$fileNanme = $_FILES['my_file']['name'] ?? null;$errorcode = $_FILES['my_file']['error'];if($errorcode > UPLOAD_ERR_OK){switch($errorcode){case UPLOAD_ERR_INI_SIZE:throw new upException("文件超过`php.ini`中`upload_max_filesize`值",1);break;case UPLOAD_ERR_FORM_SIZE:throw new upException("文件大小超过表单中`MAX_FILE_SIZE`指定的值",2);break;case UPLOAD_ERR_PARTIAL:throw new upException("文件只有部分被上传",3);break;case UPLOAD_ERR_NO_FILE:throw new upException("没有文件被上传",4);break;case UPLOAD_ERR_NO_TMP_DIR:throw new upException("找不到临时文件夹",6);break;case UPLOAD_ERR_CANT_WRITE:throw new upException("文件写入失败",7);break;default:// 测试时建议关掉default: 避免误报影响throw new upException('未知错误', 111);}}$fileType = $_FILES['my_file']['type'] ?? null;$type = strstr($fileType, '/', true); // "image"if (!is_null($fileType)) {if ($type !== 'image') throw new upException('文件类型错误',120);}$fileTmpNanme = $_FILES['my_file']['tmp_name'] ?? null;// is_uploaded_file():检查指定的文件是否是通过 HTTP POST 上传的// echo $fileTmpNanme ;// echo is_uploaded_file($fileTmpNanme);if($fileTmpNanme && is_uploaded_file($fileTmpNanme)){//文件名$orgename = $_FILES['my_file']['name'] ?? null;$ftype = strstr($orgename, '.');//文件保存地址$upload = "./upload";// is_dir() 函数检查指定的文件是否是目录。if(!is_dir($upload)){mkdir($upload,0777,true);}//保存到服务器的文件地址$upfile = $upload."/".time()."dddd".$ftype;if(move_uploaded_file($fileTmpNanme,$upfile)){echo '<img src="'.$upfile.'" alt="上传的图片" style="width: 100px;"><span style="color: rosybrown;">图片上传成功</span>';}else{echo "上传失败";}}}catch(upException $up){echo $up;}?><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>单文件上传</title></head><body><form action="" method="POST"" enctype="multipart/form-data"><fieldset><legend>单文件上传</legend><!-- 前端设置上传文件大小,必须写到input:file之前 --><input type="hidden" name="MAX_FILE_SIZE" value="38000"><input type="file" name="my_file"><input type="submit" value="提交"></fieldset></form></body></html>
文件位置截图:
前端截图:
代码部分:
<?php// printf('<pre>%s</pre>', print_r($_FILES, true));class upException extends Exception{public function __toString(){return <<<UPE<table><tr><td>错误编号</td><td>错误信息</td><td>错误文件</td><td>错误行号</td></tr><tr><td>$this->code</td><td>$this->message</td><td>$this->file</td><td>$this->line</td></tr></table><style>table{text-align: center;border: 2px solid #000;border-collapse: collapse;}td { border:1px solid black;padding: 10px;}tr:first-of-type {background-color:#eee;}tr:last-of-type td {color:lightsalmon;background-color:lightskyblue;}</style>UPE;}}$num = 0;foreach($_FILES as $file){try{$fileNanme = $file['name'] ?? null;$errorcode = $file['error'];if($errorcode > UPLOAD_ERR_OK){switch($errorcode){case UPLOAD_ERR_INI_SIZE:throw new upException("文件超过`php.ini`中`upload_max_filesize`值",1);break;case UPLOAD_ERR_FORM_SIZE:throw new upException("文件大小超过表单中`MAX_FILE_SIZE`指定的值",2);break;case UPLOAD_ERR_PARTIAL:throw new upException("文件只有部分被上传",3);break;case UPLOAD_ERR_NO_FILE:throw new upException("没有文件被上传",4);break;case UPLOAD_ERR_NO_TMP_DIR:throw new upException("找不到临时文件夹",6);break;case UPLOAD_ERR_CANT_WRITE:throw new upException("文件写入失败",7);break;default:// 测试时建议关掉default: 避免误报影响throw new upException('未知错误', 111);}}$fileType = $file['type'] ?? null;$type = strstr($fileType, '/', true); // "image"if (!is_null($fileType)) {if ($type !== 'image') throw new upException('文件类型错误',120);}$fileTmpNanme = $file['tmp_name'] ?? null;// is_uploaded_file():检查指定的文件是否是通过 HTTP POST 上传的if($fileTmpNanme && is_uploaded_file($fileTmpNanme)){//文件名$orgename = $file['name'] ?? null;$ftype = strstr($orgename, '.');//文件保存地址$upload = "./uploads";// is_dir() 函数检查指定的文件是否是目录。if(!is_dir($upload)){mkdir($upload,0777,true);}//保存到服务器的文件地址$upfile = $upload."/one".time().$num.$ftype;if(move_uploaded_file($fileTmpNanme,$upfile)){$num++;echo '<img src="'.$upfile.'" alt="上传的图片" style="width: 100px;"><span style="color: rosybrown;">图片上传成功</span>';}else{echo "上传失败";}}// else{// throw new upException("文件上传方式不正确",119);// }}catch(upException $up){echo $up;}}?><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>单文件上传</title></head><body><form action="" method="POST"" enctype="multipart/form-data"><fieldset><legend>单文件上传</legend><!-- 前端设置上传文件大小,必须写到input:file之前 --><!-- <input type="hidden" name="MAX_FILE_SIZE" value="38000"> --><input type="file" name="pic1"><input type="file" name="pic2"><input type="file" name="pic3"><input type="submit" value="提交"></fieldset></form></body></html>
文件位置截图:

前端截图:

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