批改状态:合格
老师批语:那个文件处理类的构造方法参数, 可以考虑使用剩余参数, 或extract()进行处理, 以简化代码
<?phprequire('fileup.php');if ($_FILES) :$file = new Fileup($_FILES, 'file');$result = $file->upload('uploadfile', 'image');file_ex($result);show($result);endif;?><!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 type="hidden" name="MAX_FILE_SIZE" value="700000"><input type="file" name="file[]"><input type="file" name="file[]"><input type="file" name="file[]"><input type="file" name="file[]"><button>上传</button></fieldset></form><form action="" method="post" enctype="multipart/form-data"><fieldset><legend>多文件上传</legend><input type="hidden" name="MAX_FILE_SIZE" value="1000000"><input type="file" name="file[]" multiple><button>上传</button></fieldset></form></body></html>
<?php/*** 文件上传类*/class Fileup{private $file_name, $file_tmp, $file_type, $file_error;public function __construct($file, $name){$this->file_name = $file[$name]['name'];$this->file_type = $file[$name]['type'];$this->file_tmp = $file[$name]['tmp_name'];$this->file_error = $file[$name]['error'];}/*** 上传文件到指定目录,并可以过滤上传的文件类型** @param [string] $path 上传文件在服务器保存的路径* @param [string] $type 指定上传的文件类型* @return array 返回关联二维数组[['name','error','url']...]*/public function upload($path, $type = null){$result = [];foreach ($this->file_error as $key => $value) {if ($value === 0) :if ($type && strstr($this->file_type[$key], '/', true) !== $type) :$result[] = ['name' => $this->file_name[$key],'error' => 8,'url' => null];continue;endif;$a = $this->file_tmp[$key];$b = "$path/" . md5(time() + rand(0, 999)). '.' . pathinfo($this->file_name[$key])['extension'];if (move_uploaded_file($a, $b)) :$result[] = ['name' => $this->file_name[$key],'error' => UPLOAD_ERR_OK,'url' => $b];else :$result[] = ['name' => $this->file_name[$key],'error' => UPLOAD_ERR_CANT_WRITE,'url' => null];endif;else :$result[] = ['name' => $this->file_name[$key],'error' => $value,'url' => null];endif;}return $result;}}/*** 文件上传异常类*/class File_exception extends Exception{public function __toString(){return '错误代码(' . $this->code . '),' . $this->message . '。<hr>';}}//返回异常并显示出来function file_ex($result){foreach ($result as $value) {try {switch ($value['error']) {case UPLOAD_ERR_INI_SIZE:throw new File_exception('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值', 1);break;case UPLOAD_ERR_FORM_SIZE:throw new File_exception('上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值', 2);break;case UPLOAD_ERR_PARTIAL:throw new File_exception('文件只有部分被上传', 3);break;case UPLOAD_ERR_NO_TMP_DIR:throw new File_exception('找不到临时文件夹', 6);break;case UPLOAD_ERR_CANT_WRITE:throw new File_exception('文件写入失败', 7);break;case 8:throw new File_exception('文件类型错误', 8);break;}} catch (File_exception $e) {echo $value['name'] . ':' . $e;}}}//预览上传成功的图片function show($result){foreach ($result as $value) {if ($value['error'] === UPLOAD_ERR_OK) :$name = $value['name'];$v_url = $value['url'];$html = <<< hh<div style="width:22%;float:left;margin-right:3%"><div>$name</div><img style="width:100%" src="$v_url" alt=""></div>hh;echo $html;endif;}echo '<div style="clear:both"></div>';}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号