欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PHP实现规范化文件上传

程序员文章站 2022-03-05 13:25:12
...

一 代码

<!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>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="830" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td colspan="3"><img src="images/bg_01.jpg" width="830" height="133" /></td>
  </tr>
   <form action="" method="post" enctype="multipart/form-data">
  <tr>
    <td width="193" rowspan="4">&nbsp;</td>
    <td width="423" height="50">选择上传文件:
      <input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
      <input type="file" name="up_picture"/>
</td>
    <td width="214" rowspan="4">&nbsp;</td>
  </tr>
  <tr>
    <td height="30" align="center"><span class="STYLE1">上传图片格式为(jpg)、大小为(2M)</span></td>
  </tr>
  <tr>
    <td height="50" align="center"><input type="image" name="imageField2" src="images/bg_09.jpg" />
    &nbsp;&nbsp;&nbsp;&nbsp;<input type="image" name="imageField3" src="images/bg_11.jpg" /></td>
  </tr>
    </form>
  <tr>
    <td height="70">
<?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!="jpeg"){
			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'])){	//判断文件是否是HTPP POST上传
				if(!move_uploaded_file($_FILES['up_picture']['tmp_name'],$path)){	//执行上传操作
					echo "上传失败";
				}else{
					echo "文件:<span class='STYLE1'>".time().strtolower(strstr($_FILES['up_picture']['name'],"."))."</span>  上传成功,大小为:<span class='STYLE1'>".$_FILES['up_picture']['size']."</span> 字节";
				}
			}else{
				echo "上传文件:<span class='STYLE1'>".$_FILES['up_pictute']['name']."</span>不合法!";
			}
		}
	}
}
?>
</td>
  </tr>
  <tr>
    <td colspan="3"><img src="images/bg_14.jpg" width="830" height="30" /></td>
  </tr>
</table>
</body>
</html>

 

二 运行结果
PHP实现规范化文件上传
            
    
    博客分类: PHP PHP文件上传 
 

 

  • PHP实现规范化文件上传
            
    
    博客分类: PHP PHP文件上传 
  • 大小: 8.8 KB
相关标签: PHP 文件 上传