标签:lower break 代码 type multi dex err tmp 数据
html表单,需要记住form的enctype属性:
<form action=‘index.php‘ method="POST" enctype="multipart/form-data"> <input type=‘file‘ name=‘file‘ /> <input type=‘submit‘ value=‘提交‘ /> </form>
php代码:
function uploadFile($fileInfo,$path=‘./upload/‘){ //判断错误号 if($fileInfo[‘error‘] == UPLOAD_ERR_OK){ //文件是否是通过HTTP POST上传的 if(is_uploaded_file($fileInfo[‘tmp_name‘])){ $ext = strtolower(pathinfo($fileInfo[‘name‘],PATHINFO_EXTENSION)); $allow_Ext = array(‘jpg‘,‘jpeg‘,‘png‘); if(in_array($ext, $allow_Ext)){ $length = 6; $newFileName = substr(md5(uniqid(microtime(true),true)),0,$length); $savePath = $path . $newFileName .‘.‘.$ext; if(move_uploaded_file($fileInfo[‘tmp_name‘], $savePath)){ return ‘success‘; }else{ return ‘file upload fail‘; } }else{ return ‘file type not allow‘; } }else{ return ‘文件不是通过HTTP POST上传的‘; } }else{ switch($fileInfo[‘error‘]){ case 1: $errMsg = ‘超出了配置文件的大小‘;//PHP.ini的upload_max_filesize的值 break; case 2: $errMsg = ‘超过了表单允许接收数据的大小‘;//表单中MAX_FILE_SIZE的值 break; case 3: $errMsg = ‘文件部分被上传‘; break; case 4: $errMsg = ‘没有文件被上传‘; break; case 6: $errMsg = ‘找不到临时文件夹‘; break; case 7: $errMsg = ‘文件写入失败‘; break; default: $errMsg = "Unknown upload error"; break; } return $errMsg; } } //var_dump($_FILES); if(isset($_FILES[‘file‘])){ echo uploadFile($_FILES[‘file‘]); }
标签:lower break 代码 type multi dex err tmp 数据
原文地址:https://www.cnblogs.com/gyfluck/p/9952424.html