<?php /* *文件上传 * * */ //var_dump($_FILES); /* 多文件上传处理 $data = $_FILES['icon']; $name = $data['name']; if (is_array($name)) { for ($i=0; $i<count($name); $i++) { echo $data['tmp_name'][$i].'<br />'; } } else { echo '单个文件上传'; } */ $mimes = ['p_w_picpath/jpeg','p_w_picpath/png','p_w_picpath/gif']; $suffixs = ['jpg','jpeg','pjpeg','png','gif']; //var_dump(upload('icon',$mimes,$suffixs,'upload',true)); function upload($key,$mimes,$suffixs,$path=null,$rand_name=false) { $data = $_FILES[$key]; if ($error = $data['error']) { switch ($error) { case UPLOAD_ERR_INI_SIZE: $msg = '超过了upload_max_filesize的值'; break; case UPLOAD_ERR_FORM_SIZE: $msg = '超过了表单中MAX_FILE_SIZE的值'; break; case UPLOAD_ERR_PARTIAL: $msg = '部分文件上传'; break; case UPLOAD_ERR_NO_FILE: $msg = '没有文件上传'; break; case UPLOAD_ERR_NO_TMP_DIR: $msg = '没有找到临时目录'; break; case UPLOAD_ERR_CANT_WRITE: $msg = '文件写入失败'; break; } return ['error'=>$error,'msg'=>$msg]; } else if ($data['size'] > pow(1024,2)*2) { $msg = '文件超过自定义的大小2M'; return ['error'=>0,'msg'=>$msg]; } if (!in_array($data['type'],$mimes)) { $msg = '不支持的MIME'; return ['error'=>0,'msg'=>$msg]; } $suffix = pathinfo($data['name'])['extension']; if (!in_array($suffix, $suffixs)) { $msg = '不支持的后缀名'; return ['error'=>0,'msg'=>$msg]; } if (!is_uploaded_file($data['tmp_name'])) { $msg = '不是上传文件'; return ['error'=>0,'msg'=>$msg]; } if ($path) { $path = rtrim($path,'/\\') . '/'; } else { $path = './'; } $path .= date('Y/m/d') . '/'; if (!file_exists($path)) { mkdir($path, 0777, true); } if ($rand_name) { $name = uniqid(). '.' . $suffix; } else { $name = $data['name']; } $pathname = $path . $name; if (move_uploaded_file($data['tmp_name'],$pathname)) { return ['error'=>200,'msg'=>$pathname]; } else { $msg = '上传失败'; return ['error'=>0,'msg'=>$msg]; } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。