代码块
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传</legend>
<label for="my_file"></label>
<input type="file" name="my_file" id="my_file">
<button type="submit">上传</button>
</fieldset>
</form>
</body>
</html>
php
<?php
//printf('<pre>%s</pre>',print_r($_FILES,true));
$file =$_FILES;
a($file);
function a($file){
ini_set('error.log','./error.log');
$error = $file['my_file']['error'];
if ($error!=0) {
switch ($error) {
case 1:
exit('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值');
case 2:
exit('上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值');
case 3:
exit('文件只有部分被上传');
case 4:
exit('没有文件被上传');
case 5:
exit('上传文件大小为0');
default:
exit('系统错误');
}
}
$fileName = $file['my_file']['name'];
$type = $file['my_file']['type'];
$tmpName = $file['my_file']['tmp_name'];
$size = $file['my_file']['size'];
if ($size/1024/1024 > 5) exit('文件大小不能超过5M');
$type = explode('/',$type)[1];
if (!in_array($type,['jpg','jpeg','png','wbmp','gif'] )) exit('文件格式只能是jpg,jpeg,png,wbmp,gif');
if (!file_exists('uploads/')) mkdir('uploads/',0777,true);
move_uploaded_file($tmpName,"uploads/".$fileName);
exit('文件上传成功');
}
效果图