<?php
$bt ="更换头像";
include("../conn.php");
include("../head.php");
include_once("../function.php");
$zt=sqlstr($_GET['zt']);
$sid = sqlstr($_COOKIE['dl']);
$uin=mysql_fetch_array(mysql_query("select * from user where sid='$sid'"));
$id=$uin['id'];
if(!$uin){
header("Location:../login.php");
exit;//没登录就跳转到登录页面
}

if($zt==1){

$uptypes=array('image/jpg', 'image/jpeg', 'image/png', 'image/pjpeg', 'image/gif', 'image/bmp', 'image/x-png' ,'image/JPG', 'image/JPEG', 'image/PNG', 'image/PJPEG', 'image/GIF', 'image/BMP', 'image/X-PNG' ); //允许上传格式
$max_file_size=1000000; //上传文件大小限制, 单位BYTE 
$destination_folder="../../image/tx/"; //上传文件路径 
$imgpreview=1; //是否生成预览图(1为生成,其他为不生成); 
$imgpreviewsize=1/2; //缩略图比例 
$file = $_FILES["file"]; 

if(!is_uploaded_file($_FILES["file"]['tmp_name'])) //是否存在文件 
{ 
$msg="<font color='red'>亲，你至少选择一张图片吧</font><br/>";
}elseif($max_file_size < $file["size"]) 
//检查文件大小 
{ 
$msg="<font color='red'>亲，头像必须小1MB哦，不然服务器吃不消啦</font><br/>";
}elseif(!in_array($file["type"], $uptypes)) 
//检查文件类型 
{ 
$msg="<font color='red'>头像图片格式格式错误啦</font><br/>";
}else{

if(!file_exists($destination_folder)) 
mkdir($destination_folder); 
$filename=$file["tmp_name"]; 
$image_size = getimagesize($filename); 
$pinfo=pathinfo($file["name"]); 
$ftype=$pinfo['extension']; 
$zname=time().".".$ftype;
$destination = $destination_folder.$zname;
if (file_exists($destination) && $overwrite != true) 
{ 
$msg="<font color='red'>有一张相同名字的头像了</font><br/>";
} elseif(!move_uploaded_file ($filename, $destination)){ 

$msg="<font color='red'>T_T程序开小差了，头像保存失败</font><br/>";
}else{

unlink("../../{$uin['tx']}");
$pinfo=pathinfo($destination); 
$fname=$pinfo['basename']; 
echo '<div id="main">头像更改成功<br/>';

echo "<br/>头像预览:<br/><br/>"; 
echo "<center><a href=\"".$destination."\" target='_blank'><img src=\"".$destination."\" width='150' height='150'"; //$imgpreviewsize 是1/2
echo " alt=\"图片预览:\r文件名:".$destination."\r上传时间:\" border='0'></a></center><br/><br/><center><a href='/m/diary/index.php'>返回>></a>　</center></div><br/>";
$imgurl='/image/tx/'.$zname;
mysql_query("UPDATE user SET tx='$imgurl' WHERE id='$id'" );

}

}


}

echo '<div id="main">'.$msg.'<p class="hl">修改我的头像</p>
<form action="tx.php?zt=1" method="post"
enctype="multipart/form-data">选择本地图片|<a href="/m/qq">使用QQ头像</a><br/></br/>选择图片:<br/>
<input type="file" name="file" id="file" /><br /><br /><font color=red>*</font>上传头像最好是<font color=red>正方形</font>的图片，图片须小于1MB。<br/> <font color=red>*如果头像上传一直失败，请在底部携手改善说明。</font> <br/><br />
<input class="libbtn" type="submit" name="submit" value="上传" />　　<a href="/m/diary/index.php">返回>></a>
</form><br/></div>';

?>

<?php include("../footer.php"); ?>