<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>文件上传</title> </head> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> 用户名:<input type="text" name="uname" /><br /> <!-- 使用隐藏域来控制文件的大小 --> <input type="hidden" name="MAX_FILE_SIZE" value="1024" /> 头 像:<input type="file" name="pic" /><br /> <input type="submit" name="sub" value="提交" /> </form> </body> </html>
<?php $file = $_FILES; //判断临时文件必须是通过http post上传过来的 if(is_uploaded_file($file[‘pic‘][‘tmp_name‘])){ //设置允许上传的格式 $allowType = array("iamge/png", "image/jpeg", "image/jpg", "iamge/gif"); $type = $file[‘pic‘][‘type‘]; if(!in_array($type, $allowType)){ exit("上传类型不允许"); } if($file[‘pic‘][‘size‘] > 1024 * 1024){ exit("上传头像不能大于1M"); } $path = "./uploads/"; $fname = time().rand(100, 999); $ext = array_pop(explode(‘.‘, $file[‘pic‘][‘name‘])); $abspath = $path.$fname.‘.‘.$ext; if(move_uploaded_file($file[‘pic‘][‘tmp_name‘], $abspath)){ echo "上传成功"; } } ?>
时间: 2024-10-16 17:34:32