<?php
	session_start();
	require_once("db/db.php");	
	require_once("common/common.php");
	//执行上传文件的
	require("FileUpload.class.php");
	$stritemlistno = trim($_REQUEST['itmno']);
	if(strlen($stritemlistno)<1)
	{
		echo "<script>window.location.href='itemimg.php';</script>";
		exit;
	}
	
	$wxfiles = "./mall_images/";
	//$stritemimg = 'mall_images/'.$stritemimg ;
	
	//这里实例化后赋值为数组，数组的下标要对应类中属性的值，否则不能传递值，可以不分先后但是必须一致
	//$up = new FileUpload(array('israndname'=>'true',"filepath"=>"./wximages/",'allowtype'=>array('txt','doc','jpg','gif'),"maxsize"=>1000000));
	$up = new FileUpload(array('israndname'=>'true',"filepath"=>$wxfiles,'allowtype'=>array('png','jpg','gif','jpeg'),"maxsize"=>1000000));
	//echo '1<pre>';
 
   if($up -> uploadFile("pic"))
   {
		$strnewfilename='';
		//print_r($up -> getNewFileName());
		//echo $up -> getNewFileName()->;
		//$speed = array(50,120,180,240,380); 
		foreach($up -> getNewFileName() as $keys=>$values)
		{
			$strnewfilename=trim($values);
			//echo $keys."=>".$values."<br />"; 
			//echo '*'.$strnewfilename.'*'; 
			//开始保存数据	
		}
		$stroldimgname='';
		if(strlen($strnewfilename)>0)
		{
			$sql = sprintf("SELECT wximage FROM TCP_Item_List Where ListNo=%s",$stritemlistno);
			$result = querySql($sql);
	
			if($row = fetch_array($result))
			{
				$stroldimgname = $row['wximage'];
			}
			freeResult($result);	//释放数据库
			
			$sql = sprintf("Update TCP_Item_List Set wximage='%s' Where ListNo=%s",$strnewfilename,$stritemlistno);
			executeSql($sql);
			closeDb();				//关闭数据库
		}
		//删除原来的wejian 
		if(strlen($stroldimgname)>0)
		{
			$stroldimgname = './mall_images/'.$stroldimgname;
			unlink ($stroldimgname);
		}
		echo "<script>window.location.href='itemimg.php';</script>";
   } 
   else
   {
		$strErrId='';
		//print_r($up -> getErrorMsg());
		foreach($up -> getErrorMsg() as $keys=>$values)
		{
			$strErrId=$values;
			//echo $keys."=>".$values."<br />"; 
			//echo '#'.$values.'#';
		}
		echo "<script>window.location.href='itmimgsave.php?itmno=".$stritemlistno."&retv=".$strErrId."';</script>";
   }
  //echo '2<pre>';
?>