现在的位置: 首页 > 综合 > 正文

php 文件上传

2013年12月06日 ⁄ 综合 ⁄ 共 2254字 ⁄ 字号 评论关闭

前两天想着用ftp来做一个php的文件上传。始终都明白,就是传不上服务器。以后有时间有待解决。做了一个http原理的。

<?php
session_start();
?> 
<!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 type="text/css">
td{ font-size:13px; line-height:24px;}
</style>
<script type="text/javascript">
function upload()
{
  var uname=document.uploadForm.uname.value;
  var upwd=document.uploadForm.upwd.value;
  var uimg=document.uploadForm.uimg.value;  
  var end=new String (uimg.substring(uimg.lastIndexOf(".")+1,uimg.length));
  if(uname=='')
  {
    alert("请输入用户名!");
	uploadForm.uname.focus();
	return false;
  }
  else if(upwd=='')
  {
     alert("请输入密码!");
	 uploadForm.upwd.focus();
	 return false;
  }
  else if(uimg=='')
  {  
     alert("请浏览头像!"); 
	 return false;
  }
  else if(end!='jpg'&&end!='png'&&end!='gif'&&end!='bmp'&&end!='')
  {
     alert("上传文件格式有误,只能上传图片格式,请核实!");
	 return false;
  } 
  return true;
}

</script>
</head>

<body>
<form name="uploadForm" id="uploadForm" action="upd.php?action=upload" method="post" enctype="multipart/form-data">
<table cellpadding="0" cellspacing="5" border="0" width="600">
<tr>
<td>用户名</td><td><input type="text" name="uname" id="uname" value="<?php echo $_SESSION['uname']?>"/></td>
</tr>
<tr>
<td>密码</td><td><input type="text" name="upwd" id="upwd" value="<?php echo $_SESSION['upwd']?>"/></td>
</tr>
<tr>
<td>头像</td><td><input type="file" name="uimg" id="uimg"/></td>
</tr>
<tr>
<td><input type="submit" name="sbt" value="提交" onclick="return upload()"/></td>
<td><input type="button" name="bt" value="重填"/></td>
</tr>
</table>
</form>
<?php
$action=$_GET['action'];
if($action=='upload')
{
   date_default_timezone_set('PRC'); 
   $Path="./upload/"; 
   if (!is_dir($Path))//创建路径 
   { 
		mkdir($Path); 
   }  
   $result=0;
   if ($_FILES["uimg"]["size"]!=0&&$_FILES["uimg"]["size"]<1024*1024) 
   { 
		$File=$Path.date('YmdHis')."_".$_FILES["uimg"]["name"]; 
		if(move_uploaded_file($_FILES['uimg']['tmp_name'],iconv('UTF-8','gb2312',$File)))//iconv(....,$File)防止中文乱码		{  
			$result=1;
		}  
		else 
		{   
			$result=2;
		} 
	} 
	else
	{
		$result=3;
	} 
	if($result!=1)
	{
	   $_SESSION['uname']=$_POST['uname'];
	   $_SESSION['upwd']=$_POST['upwd'];
	   $message="上传失败!";
	   if($result==3)
	   {
	     $message="图片不能超过1MB,请核实!";
	   }
	   ?>
	   <script type="text/javascript">
	   alert('<?php echo $message?>');
	   location.href='upd.php';
	   </script>
	   <?php
	}
	else
	{
	  //正确,处理。。。。。。
	}
}
?>
</body>
</html>

抱歉!评论已关闭.