<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>规范的文件上传</title> <style> *{ margin:0 auto; } .div1{ 830px; height:133px; background-image:url(images/bg_111.jpg); } .div2{ 830px; height:30px; background-image:url(images/bg_110.jpg); } #upload { 830px; height:133px; text-align:center; margin-top:100px; } </style> </head> <body> <div class="div1"> </div> <div id="upload"> <form method="post" name="form1" action="index.php" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="100000000" /> 选择上传文件: <input type="file" name="up_picture" /> <input type="image" name="imageField2" src="images/bg_00.jpg" /> <input type="image" name="imageField3" src="images/bg_000.jpg" /> </form> </div> <center> <?php if(!empty($_FILES['up_picture']['name'])){ //判断上传内容是否为空 if($_FILES['up_picture']['error']>0){ //判断文件是否可以上传到服务器 echo"上传错误"; switch($_FILES['up_picture']['error']){ case 1: echo"上传文件大小超出配置文件规定值"; break; case 2: echo"上传文件大小超出表单中约定值"; break; case 3: echo"上传文件不全"; break; case 4: echo"没有上传文件"; break; } }else{ list($maintype,$subtype)=explode("/",$_FILES['up_picture']['type']); if($maintype!="image"&&$subtype!="pjpeg"){ echo"上传文件格式不正确"; }else{ if(!is_dir("./upfile/")){ //判断指定目录是否存在 mkdir("./upfile/"); //创建目录 } $path='./upfile/'.time().strtolower(strstr($_FILES['up_picture']['name'],".")); //定义上传文件的名字和上传位置 if(is_uploaded_file($_FILES['up_picture']['tmp_name'])){ //判断文件是否是http.post上传 if(!move_uploaded_file($_FILES['up_picture']['tmp_name'],$path)){ //执行上传操作 echo"上传失败"; }else{ echo"上传成功"; } }else{ echo"上传文件不合法"; } } } } ?> </center> <div class="div2"> </div> </body> </html>