<?php
    session_start(); 
	require_once('inc/conn.php');
	require_once('inc/constant.php');
	require_once('inc/ls_syscode.php');
	require_once('inc/check.php');
	$opendb=new mydb();
	require_once('inc/char_set.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>savebatchadd</title>
</head>
<body>
<?
	$itemnoprefix=$_POST['itemnoprefix'];
	$productname=$_POST['productname'];
	$id=$_POST['id'];//类别id
	$thisBigPicW=$_POST['thisBigPicW'];
	$thisBigPicH=$_POST['thisBigPicH'];
	$opendb=new mydb();
	$productSpecification=htmlspecialchars($_POST['productSpecification'], ENT_QUOTES);
	$firstfile=$_FILES['userfile']['tmp_name'][0];
	if($firstfile!="") addproducts($productname,$_FILES);
	$showonhome=0;
	$j=1;
	
	$source=trim($_POST['folder']);
	if($source!=""){   
		if(!is_dir($source ) )echo(" $source 不是目录!<br/>");   
	}   
	else{
		$hasfolder=true;
		$handle=dir($source); 
		$k=0;	
		$l=0;
		while($entry=$handle->read()) {   
			if(($entry!=".")&&($entry!="..")){   
				if(is_dir($source."/". $entry)){ 
					$cateid=addcate( $entry); $i++;
					$handle11=dir( $source."/". $entry); 
					while($entry11=$handle11->read()) {   
						if(($entry11!=".")&&($entry11!="..")){   
							  if(is_dir($source."/". $entry."/". $entry11)){ 
								  $midcateid=addmidcate( $entry, $entry11);  $j++;
								  //中类下图片
								  $handle111=dir( $source."/". $entry."/". $entry11); 
									while($entry111=$handle111->read()) { 
									  	if(is_dir($source."/". $entry."/". $entry11."/". $entry111))  continue;
										else{
											$_FILES['userfile']['tmp_name'][$i]= $entry;
											$_FILES['userfile']['name'][$i]= $entry;
											$productname=$entry111; //产品名 取中类名
					  						addproducts( $entry11,$_FILES,$entry111); //多个大类文件夹,多个中类文件夹
										
										}
								  
							  }else{
									$_FILES['userfile']['tmp_name'][$i]= $entry11;
									$_FILES['userfile']['name'][$i]= $entry11;
									$productname=$entry11; //产品名 取大类名
					  				addproducts( $entry11,$_FILES); //多个大类文件夹
							  }
						 }
					 }

				}else{
					  $_FILES['userfile']['tmp_name'][$i]= $entry;
					  $_FILES['userfile']['name'][$i]= $entry;
					  addproducts($productname,$_FILES); //单独大类；产品名 取大类名
				}
			}//if(($entry!=".")&&($entry!="..")){  
		}//while($entry=$handle->read()) { 
	}

addproducts( $catename,$_FILES,$midcatenamem){
	global $productname,$itemnoprefix,$id,$thisBigPicW,$thisBigPicH,$productSpecification,$showonhome,$j;
	for($i=0;$i<$BatchFiles;$i++) 
	{
			$select_sql2="select * from ls_products where productID=".$id." ";
			$select_query2=$opendb->query($select_sql2);
			$select_rows2=mysql_num_rows($select_query2); //总产品数
			$no=$select_rows2+1;
			$productItemNo=$itemnoprefix.$no;
			$productName=$productname;
			$file=$_FILES['userfile']['tmp_name'][$i];
			if($file=="") 			//文件不存在就找下一个
			{continue;}
			
			list($width, $height, $type, $attr) = getimagesize("$file");
			$image=$_FILES['userfile']['name'][$i];   
  			$size=filesize($file);   
			$pathFoldersProducts="../".getproductfolder($id);
			if ($type ==1  || $type ==2  || $type ==3  )//1="image/gif" 2="image/png" 3="image/pjpeg"
			{
				 if($size < $PicSize) //开始判断图像大小
				{
					$pathBigPic=$pathFoldersProducts."/".$BigFoldersProducts;
					//echo $pathBigPic."<br>";
					//创建大图文件夹
					createFolder($pathBigPic);
					$pathSmallPic=$pathFoldersProducts."/".$SmallFoldersProducts;
					//echo $pathSmallPic."<br>";
					//创建小图文件夹
					createFolder($pathSmallPic);
					$pathPagePic=$pathFoldersProducts;
					//echo $pathSmallPic."<br>";
					//创建大图浏览（.htm）文件夹
					createFolder($pathPagePic);
						
					
					$arr=array(" ",".");   
					//产生文件名   
					$picfilename=time()*100+$j;
					$productPic=str_replace($arr ,"",$picfilename).".".substr($image,-3);
					//echo $productPic;
					$pathBigPic=$pathBigPic."/".$productPic;
					//上传文件： 文件夹使用 rename 移动，上传文件 用 move_uploaded_file
					if($hasfolder)copy ( $file, $pathBigPic)else move_uploaded_file($file, $pathBigPic);
					
					$srcFile=$pathBigPic; //源地址
					$toW=$SmallPicW;			//宽度
					$toH=$SmallPicH;			//高度
					$toFile=$pathSmallPic."/".$productPic; //目标地址
					//生成缩略图
					ImageResize($srcFile,$toW,$toH,$toFile);
					if($width>$thisBigPicW||$height>$thisBigPicH)//大图超过预定长宽，缩小
					{
						$toW=$thisBigPicW;
						$toH=$thisBigPicH;
						$srcFile=$pathBigPic;
						$toFile=$pathBigPic;
						ImageResize($srcFile,$toW,$toH,$toFile);
					}
					$insert_sql=sprintf("insert into ls_products 						(productPic,productItemNo,productName,productSpecification,productID,Num,showOnHome,enable) values (%s,%s,%s,%s,%s,%s,%s,%s) ",
							GetSQLValueString($productPic, "text"),
							GetSQLValueString($productItemNo, "text"),
							GetSQLValueString($productName, "text"),
							GetSQLValueString($productSpecification, "text"),
							GetSQLValueString($id, "int"),
							GetSQLValueString($select_rows2+1, "int"),
							GetSQLValueString($showonhome, "int"),
							GetSQLValueString("1", "int")
						 );
						//echo $insert_sql;
						//添加数据
						$insert_query=$opendb->query($insert_sql);
					}
				
		}
	echo "生成： ".$j.". ".$pathBigPic."<br/><br/>";
	$j++;

	}//end while
	echo "<br/><a href=\"cate.php\">返回</a>";
	$opendb->close();
} //addproducts
	
	//添加大类
function addcate( $cateName ){  
			$cateFolders="../".$cateName;
			createFolder($cateFolders);
			$select_sql2="select * from ls_products where cateName='".$cateName."' ";
			$select_query2=$opendb->query($select_sql2);
			$select_rows2=mysql_num_rows($select_query2); //总产品数
			if($select_rows==0){
				select_rows2=getcatetotal(); //类别数
				$insert_sql=sprintf("insert into ls_products 												(cateName,cateFolders,cateTitle,cateKeywords,cateDescription,catePagePic,catePagePicWidth,catePagePicHeight,cateIconPic,cateBrief,cateDetail,Num,enable,date) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ",
						GetSQLValueString($cateName, "text"),
						GetSQLValueString($cateFolders, "text"),
						GetSQLValueString($cateName, "text"),
						GetSQLValueString($cateName, "text"),
						GetSQLValueString($cateName, "text"),
						GetSQLValueString($catePagePic, "text"),
						GetSQLValueString($catePagePicWidth, "text"),
						GetSQLValueString($catePagePicHeight, "text"),
						GetSQLValueString($cateIconPic, "text"),
						GetSQLValueString($cateBrief, "text"),
						GetSQLValueString($cateDetail, "text"),
						GetSQLValueString($select_rows2+1, "int"),
						GetSQLValueString("1", "int"),
						GetSQLValueString(date('Y-m-d H:i:s'), "text")
				);
				$select_query2=$opendb->query($insert_sql);
				return $opendb->insert_id();
			}

}
	
	//添加中类

function addmidcate( $cateName, $midCateName){
			$midcateFolders="../".$cateName."/".$midCateName;
			createFolder($midcateFolders);
			$select_sql2="select * from ls_products where cateName='".$cateName."' ";
			$select_query2=$opendb->query($select_sql2);
			$select_rows2=mysql_num_rows($select_query2); //总产品数
			if($select_rows==0){
				select_rows2=getcatetotal($cateName); //中类数
				$insert_sql=sprintf("insert into ls_products (cateTitle,cateKeywords,cateDescription,midcateName,midcateFolders,midcateBrief,catePic,midcateDetail,midcateID,Num,enable,date) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ",
				  GetSQLValueString($midCateName, "text"),
				  GetSQLValueString($midCateName, "text"),
				  GetSQLValueString($midCateName, "text"),
				  GetSQLValueString($midcateName, "text"),
				  GetSQLValueString($midcateFolders, "text"),
				  GetSQLValueString($midcateBrief, "text"),
				  GetSQLValueString($picName, "text"),
				  GetSQLValueString($midcateDetail, "text"),	
				  GetSQLValueString($midcateID, "text"),
				  GetSQLValueString($select_rows2+1, "int"),
				  GetSQLValueString("1", "int"),
				  GetSQLValueString(date('Y-m-d H:i:s'), "text")
			   );
				$select_query2=$opendb->query($insert_sql);
				return $opendb->insert_id();
			}		
}

?>
</body>
</html>
