<?php
session_start();
ob_start();
include_once("../dbconnect.php");//Database connection

	/*function createthumb($name, $newname, $new_w, $new_h, $border=false, $transparency=true, $base64=false) {

    if(file_exists($newname))
	{
        @unlink($newname);
		
	}	
    if(!file_exists($name)){
	
        return false;
		
		}
    $arr = split("\.",$name);
    $ext = $arr[count($arr)-1];

    if($ext=="jpeg" || $ext=="jpg" || $ext=="JPEG" || $ext=="JPG" ){
        $img = @imagecreatefromjpeg($name);
		
    } elseif($ext=="png"){
        $img = @imagecreatefrompng($name);
    } elseif($ext=="gif") {
        $img = @imagecreatefromgif($name);
    }
    if(!$img)
        return false;
    $old_x = imageSX($img);
    $old_y = imageSY($img);
	$thumb_w = $new_w;
    $thumb_h = $new_h;
  
    $thumb_w = ($thumb_w<1) ? 1 : $thumb_w;
    $thumb_h = ($thumb_h<1) ? 1 : $thumb_h;
    $new_img = ImageCreateTrueColor($thumb_w, $thumb_h);
   
    if($transparency) {
        if($ext=="png") {
            imagealphablending($new_img, false);
            $colorTransparent = imagecolorallocatealpha($new_img, 0, 0, 0, 127);
            imagefill($new_img, 0, 0, $colorTransparent);
            imagesavealpha($new_img, true);
        } elseif($ext=="gif") {
            $trnprt_indx = imagecolortransparent($img);
            if ($trnprt_indx >= 0) {
                //its transparent
                $trnprt_color = imagecolorsforindex($img, $trnprt_indx);
                $trnprt_indx = imagecolorallocate($new_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                imagefill($new_img, 0, 0, $trnprt_indx);
                imagecolortransparent($new_img, $trnprt_indx);
            }
        }
    } else {
        Imagefill($new_img, 0, 0, imagecolorallocate($new_img, 255, 255, 255));
    }
   
    imagecopyresampled($new_img, $img, 0,0,0,0, $thumb_w, $thumb_h, $old_x, $old_y);
    if($border) {
        $black = imagecolorallocate($new_img, 0, 0, 0);
        imagerectangle($new_img,0,0, $thumb_w, $thumb_h, $black);
    }
    if($base64) {
        ob_start();
        imagepng($new_img);
        $img = ob_get_contents();
        ob_end_clean();
        $return = base64_encode($img);
    } else {
        if($ext=="jpeg" || $ext=="jpg" || $ext=="JPEG" || $ext=="JPG" ){
            imagejpeg($new_img, $newname);
            $return = true;
        } elseif($ext=="png"){
            imagepng($new_img, $newname);
            $return = true;
        } elseif($ext=="gif") {
            imagegif($new_img, $newname);
            $return = true;
        }
    }
    imagedestroy($new_img);
    imagedestroy($img);
    return $return;
}*/

	
	


$dbtab = "products";
$fieldseparator = ',';
$lineseparator = "\n";
//change the name of the file you want
$upload = "uploads/";
$res = move_uploaded_file($_FILES['csv']['tmp_name'],$upload.basename($_FILES['csv']['name']));
$csvfile = $upload. $_FILES['csv']['name'];

/********************************************************************************************/
/* Would you like to add an empty field at the beginning of these records?
/* This is useful if you have a table with the first field being an auto_increment integer
/* and the csv file does not have such as empty field before the records.
/* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
/* This can dump data in the wrong fields if this extra field does not exist in the table
/********************************************************************************************/
$addauto = 0;
/********************************************************************************************/
if(!file_exists($csvfile)) {
$msg = "File not found. Make sure you specified the correct path.
";
}
$file = fopen($csvfile,"r");

if(!$file) {
$msg = "Error opening data file.
";
}

$size = filesize($csvfile);

if(!$size) {
$msg = "File is empty.
";
}
$csvcontent = fread($file,$size);

fclose($file);
$lines = 0;
$queries = "";
$inserted = 0;
$linearray = array();


foreach(split($lineseparator,$csvcontent) as $line) {
$lines++;
  $line = trim($line,' \t');
  
 $line = str_replace("\\r",'',$line);
 
/************************************************************************************************************
This line escapes the special character. remove it if entries are already escaped in the csv file
************************************************************************************************************/
  $line = str_replace("'","\'",$line);
/***********************************************************************************************************/

$linearray = explode($fieldseparator,$line);

if($lines == 1)
{
//first line as the name of the fields
 $first = explode($fieldseparator,$line);
}
if($lines !=1)
{

if($_POST['catid']!="")
{
	 $catid=$_POST['catid'];
}
else
{
	$catid=0;
}
if($_POST['cattype']!="")
{
	$cattype=$_POST['cattype'];
}
else
{
	$cattype=0;
}


// $linemysql = implode(',',$linearray);
if($linearray[1]!="") {

if($linearray[0]!="")
 {
$sql="update products set itemno='".$linearray[1]."',pname='".$linearray[2]."',unitsprice='".$linearray[3]."',colors='".$linearray[4]."',description='".$linearray[5]."',discount='".$linearray[6]."',mini='".$linearray[7]."',maxi='".$linearray[8]."',weight='".$linearray[10]."',sizes='".$linearray[11]."',Print='".$linearray[12]."',

 where pid='".$linearray[0]."'";
 $queries .= $sql . "\n";
$res = mysql_query($sql);

$lastid=$linearray[0];

}
else
{
 $sql= "INSERT INTO products(catid,itemno,pname,unitsprice,colors,description,discount,mini,maxi,status,addedon,
weight,sizes,Print) VALUES('".$_POST['catid']."','".$linearray[1]."','".$linearray[2]."','".$linearray[3]."','".$linearray[4]."','".$linearray[5]."','".$linearray[6]."','".$linearray[7]."','".$linearray[8]."',1,'".$linearray[9]."','".$linearray[10]."','".$linearray[11]."','".$linearray[12]."')";
$queries .= $sql . "\n";
$res = mysql_query($sql);
$lastid=mysql_insert_id();
}
 
   

	

	
	        //$lastid=143;
// $fname ="../upload/".rtrim(ltrim($linearray[22]));
 
 // createthumb($fname, "../products/".$lastid.".jpg", 114,152,false, true, false);
//createthumb($fname, "../products/big/".$lastid.".jpg", 284,379,false, true, false);

 
	}		
if($res){

$inserted ++;
}
}
}
 $inserted; 
   $lines;
  $rec = $lines-2;

     $msg = "Found a total of $rec  records in this csv file.Data inserted successfully";
//header("location:addsubcategory.php?catid=".$_POST['catid']."&subcat=".$_POST['subcat']."&msg=".($msg));

header("Location:catlastsubsub.php?catid=".$_POST['catid']."&msg=".($msg)); 


?>