<?php
require_once('../../include/class.inc.php');

	$ID=$_GET["ID"];
	$fid=intval($_POST["fid"]);
	$title=htmlspecialchars($_POST["title"]);
	$content=htmlspecialchars($_POST["content"]);
	$Home_t=intval($_POST["Home_t"]);
	$hits=intval($_POST["hits"]);
	if ($_POST["user"]) {
		$user=$_POST["user"];
		$user="'$user'";
	} else {
		$user="NULL";
	}
	
	
if (!empty($_FILES['image']['size'])){
//检查阶段+-----------------------------------------------------------------------------------------------
	if ($_POST['submitted'] == "yes"){
				$goodtogo = true;
				//检查图片是否是0长度.
				try {
					if ($_FILES['image']['size'] == 0){
						$goodtogo = false;
						throw new exception ("对不起,不能上传其它格式图片.");
					}
				} catch (exception $e) {
					echo $e->getmessage();
				}
				//检查文件大小.
				try {
					if ($_FILES['image']['size'] > 500000){
						$goodtogo = false;
						//显示错误信息.
						throw new exception ("对不起,图片文件不能超过: " . intval ($_FILES['image']['size'] / 1000) . "KB");
					}
				} catch (exception $e) {
					echo $e->getmessage();
				}
				//允许上传的mime类型,本例中只允许jpg和gif格式,您可自行添加
				$allowedmimes = array ("image/jpeg","image/pjpeg","image/gif","image/jpg");
				try {
					if (!in_array ($_FILES['image']['type'],$allowedmimes)){
						$goodtogo = false;
						throw new exception ("错误,文件类型必须为 .jpg或是 .gif文件. 我们上传的文件类型为: " . $_FILES['image']['type'] . "");
					}
				} catch (exception $e) {
					echo $e->getmessage ();
				}
				//如果通过提交,则移动到目录中,显示之.
				if ($goodtogo){
					try {
//检查阶段----------------------------------------------------------------------------------------------
//物理删除阶段----------------------------------------------------------------------------------------------
						$sql="select * from news where ID=".$ID;//查要修改的原数据
						$result=mysql_query($sql);//执行sql
						$rs=mysql_fetch_array($result);//提取数组
						$lujing="../".$rs['img'];//定义图片的完整路径
						if(file_exists($lujing)){//判断图片文件是否存在
							@unlink($lujing);//物理删除
							echo "原图物理删除成功 ";//提示成功
						}
											
//物理删除阶段----------------------------------------------------------------------------------------------
//上传阶段----------------------------------------------------------------------------------------------
						$pic=$_FILES['image']['name'];	//上传文件总终变量，路径+文件名					
						$date=date('Ymdhis');  //取当前时间
						$name=explode('.',$pic);//将文件名以'.'分割得到后缀名,得到一个数组 
						$newPath=$date.'.'.$name[1]; //得到一个新的文件为'20070705163148.jpg',即新的路径 
						$picName="uploads/".$newPath;	
						//$oldPath=$_FILES['file']['tmp_name'];//临时文件夹,即以前的路径 
						//rename($oldPath,$newPath); 就可以重命名了!							
							if (!move_uploaded_file ($_FILES['image']['tmp_name'],"../uploads/".$newPath)){
								$goodtogo = false;
								throw new exception ("上传文件的uploads目录有错误,请检查属性.");
							}
					} 
						catch (exception $e) {
							echo $e->getmessage ();
						}
				}

		
			}
//上传阶段----------------------------------------------------------------------------------------------

}else{
	$picName=$_POST['img'];
}		

		$sql="update news set typeid=$fid,title='$title',content='$content',Home_t=$Home_t ,hits=$hits,user=$user,adddate=now(),img='$picName' where ID='$ID'";
		//echo $sql;
//		exit;
		$result=$my->data_work($sql); 
		if($result)
		{
			echo ("<script type='text/javascript'>alert('修改成功！！');location.href='news.php';</script>");
		}
		else
		{
			echo ("<script type='text/javascript'>alert('修改失败！！');location.href='news_up.php?ID=".$ID."';</script>");
		}

?>