<?php
	require_once('_approot.php');	
?>
<!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">
		<link href="css/admin.css" rel="stylesheet" type="text/css">
		<title>无标题文档</title>
	</head>

	<body>
<?PHP

	$ls=$_POST['ls'];
	$id=$_POST['id'];
	$selectcate=$_GET['selectcate'];
	
	$title=$_POST['newsTitle'];
	$keywords=$_POST['newsKeywords'];
	$description=$_POST['newsDescription'];

	$downloadTitle=$_POST['downloadTitle'];
	$cateName=$_POST['cateName'];
	if($cateName==""){$cateName=' ';}
	
	if($downloadTitle=="")
	{
		echo "<script>alert('请填写标题！');history.go(-1);</script>";
		break;
	}
	

	$ChangecatePagePic=$_POST['ChangecatePagePic'];
	$image=$_FILES['downloadName']['name'];   
	 
  $size=$_FILES['downloadName']['size']==0?1:$_FILES['downloadName']['size'];   
  $type=$_FILES['downloadName']['type'];
  
  
  
  //echo $ChangecatePagePic;exit;
  //echo "名字：".$image."<br>";
  //echo "大小：".$size."<br>";
  //echo "类型：".$type."<br>";
	
	
	
	switch ($ls)
	{
		case "insert":
				  
			
				 if($size < $downloadSize*1000000) //开始判断图像大小
					{
						$pathBigPic="../".$downloadFolders;
							//echo $pathBigPic."<br>";
							//创建大图文件夹
						createFolder($pathBigPic);
																			
						$arr=array(" ",".");   
						//产生文件名   
						//$productPic=str_replace($arr ,"",time()).".".substr($image,-3); 时间戳文件名
						//$productPic=clearFolder($image,false);
						if(trim($image)=="")  $image="nofile";
						$productPic=$image;
						
						$pathBigPic=$pathBigPic."/".$productPic;
						//echo $pathBigPic."<br>";
						//上传文件
						move_uploaded_file($_FILES['downloadName']['tmp_name'], $pathBigPic);
						
						$select_sql2="select * from ls_download ";
						$select_query2=$opendb->query($select_sql2);
						$select_rows2=mysqli_num_rows($select_query2); //总数
						
						$insert_sql=sprintf("insert into ls_download(cateName, newsTitle, newsKeywords, newsDescription, downloadTitle,downloadName,downloadSize,enable,Num) values(%s,%s,%s,%s,%s,%s,%s,%s,%s)",
						
						GetSQLValueString($cateName, "text"),
						GetSQLValueString($title, "text"),
						GetSQLValueString($keywords, "text"),
						GetSQLValueString($description, "text"),
						GetSQLValueString($downloadTitle, "text"),
						GetSQLValueString($productPic, "text"),
						GetSQLValueString($size, "int"),
						
						GetSQLValueString("1", "int"),
						GetSQLValueString(($select_rows2+1), "int"));
						$insert_query=$opendb->query($insert_sql);
						if($image=="nofile")
						{
							echo "<script>alert('没有上传文件！');history.go(-1);</script>";
							break;
						}
						echo "<script>location='download.php?selectcate=".$selectcate."';</script>";
				
					}						
				 else
					{
						echo "	<script>alert('文件大小超过了限制，最大只能上传".$downloadSize."M 的文件！');history.go(-1);</script>";
					}
		 
			break;
		
	
		case "update":
					
					  if($ChangecatePagePic=="yes")
	  				 	{
	  				 		
											if($size < $downloadSize*1000000) //开始判断图像大小
											{
													  $pathBigPic="../".$downloadFolders;
						
						
														$arr=array(" ",".");   
													  	//产生文件名   
													  	$pic=$_POST['Pic'];
													  	$pic=trim(str_replace(substr($image,-4),"",$pic));
													  	//echo $pic;
													  	$productPic=str_replace($arr ,"",$pic).".".get_extension($image);//substr($image,-3);
														if(trim($image)!="")	$productPic=trim($image);
													  														
													 
													  	$pathBigPic=$pathBigPic."/".$productPic;
													  	//echo $pathBigPic;
													  	//上传文件
													  	move_uploaded_file($_FILES['productPic']['tmp_name'], $pathBigPic);
														
													  	$update_sql=sprintf("update ls_download set downloadTitle=%s,newsTitle=%s,newsKeywords=%s,newsDescription=%s, downloadSize=%s,downloadName=%s where ID=%s",
                                                                  GetSQLValueString($downloadTitle, "text"),
                                                                   GetSQLValueString($title, "text"),
                                                                    GetSQLValueString($keywords, "text"),
                                                                    GetSQLValueString($description, "text"),
                                                                      GetSQLValueString($size, "int"),
																	   GetSQLValueString($productPic, "text"),
                                                                        GetSQLValueString($id, "int")
                                                                );
														  $update_query=$opendb->query($update_sql);
														  echo "<script>alert('数据更新成功!');location='download.php?selectcate=$selectcate';</script>";
														 
													  		
								}
											else
											{
												echo "	<script>alert('文件大小超过了限制，最大只能上传".$downloadSize."M的文件！');history.go(-1);</script>";
											}
									 
	  				 	} 
	  				 	else
						{
							$update_sql=sprintf("update ls_download set downloadTitle=%s, newsTitle=%s,newsKeywords=%s,newsDescription=%s where ID=%s",
                                                    				GetSQLValueString($downloadTitle, "text"),
                                      		                        GetSQLValueString($title, "text"),
                                                                    GetSQLValueString($keywords, "text"),
                                                                    GetSQLValueString($description, "text"),
																	 
                                                                    GetSQLValueString($id, "int")
                                                                );
							  $update_query=$opendb->query($update_sql);
														  echo "<script>alert('数据更新成功!');location='download.php?selectcate=$selectcate';</script>";
	  				 								
						}
															
															
														  //echo $update_sql;
														  //更新数据
														
				
		break;	
	}

?>
	</body>
</html>