<?php
/********
Amitai Sasson

grabFile2.php
This is the file that does
all the cleanup, stores the file and uploads 
the information into the database.
*********/
?>

<html> 
<head> 
<title>overstockArt Amazon SKU Creation dump file </title> 
</head> 
<body bgcolor="#FFFFFF">
<?php echo '<a href="' . $path . 'index.php">'; ?> 
<?php echo '<img border="0" src="' . $path .'images/SORTER.gif"></a>'; ?> 
<table border="0" cellpadding="0" cellspacing="0" bordercolor="#111111" width="100%">
<tr> 
<td width="100%" bgcolor="#293b74" height="22" colspan="2"> 
<p style="margin-left: 10"><b><font face="Verdana" size="2" color="#FFFFFF"> 
Summary of the Items Feed File</font></b></td>
</tr> 
<?php

// GrabFile.php: Takes the details 
// of the new file posted as part 
// of the form and adds it to the 

include "config.php";

//global $strDesc; 
/*global $fileUpload; 
global $fileUpload_name; 
global $fileUpload_size; 
global $fileUpload_type;*/

//echo $HTTP_POST_VARS["frmUploadFile"];

ini_set("display_errors","On");
error_reporting(E_ALL ^ E_NOTICE); //error_reporting(0); //
// Make sure both a description and 
/*// file have been entered 
if($fileUpload == "none")  //empty($strDesc) ||
die("You must enter a file");*/
//$timestamp = time();
$fileUpload = $_FILES['fileUpload'];
$filenew=$_FILES['fileUpload']['name'];
if ($filenew) 
 { 
$filename = "feeds/amazon-onsite.txt";
move_uploaded_file($fileUpload['tmp_name'], $filename);
 }
// Database connection variables 
$dbServer = $mysqlserver; 
$dbDatabase = $mysqldatabase; 
$dbUser = $mysqlusername; 
$dbPass = $mysqlpassword;

$fileHandle = fopen($filename, "r"); 
$arrContent = file( $filename );
$numLines = count( $arrContent ); // Count the elements in the array

$row = 0; //number of rows
$count2images = 0; //count the number of products with two images
$sku_count = 0; //count the number of products
$frame_count = 0; //count the number of frames offered

$sConn = mysql_connect($dbServer, $dbUser, $dbPass) 
or die("Couldn't connect to database server"); 

$dConn = mysql_select_db($dbDatabase, $sConn) 
or die("Couldn't connect to database $dbDatabase");

//insert into database
$dbSet = "DROP TABLE IF EXISTS amazon_dropdhip";
mysql_query($dbSet) or die("Couldn't add file to database");

$dbQuery = "CREATE TABLE `amazon_dropship` (
			`Primary` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
			`SKU` VARCHAR( 54 ) NOT NULL,
			`amazonSKU` VARCHAR( 54 ) NOT NULL,
			`Name` VARCHAR( 256 ),
			`Status` VARCHAR( 56 ),
			`Batch` INT( 24 )
			) TYPE = MYISAM ;";
mysql_query($dbQuery) or die("Couldn't create table to database");

while (($data = fgetcsv($fileHandle, 10000, "	")) !== FALSE) {
    $numContent = count($data);
	//echo "data: " . $numContent;
			if ($row > 0 && $row !=($numLines)) //SKIP 1ST 2 LINES AND THE LAST LINE
			{
				 if (isset($data[1]))
				 $amazonSKU = trim($data[1]);
				 else $amazonSKU = "";

				if (isset($data[0]))
				 $SKU = trim($data[0]," ");
				 else $SKU = "";
				 
				 if (isset($data[2]))
				 $Name = addslashes($data[2]);
				 else $Name = "";
 
				 if (isset($data[3]))
				 $Status = addslashes($data[3]);
				 else $Status = "";
				 
				 if (isset($data[4]))
				 $Batch = addslashes($data[4]);
				 else $Batch = "";

				// $description1 = substr($description,0,strpos($description,'%100'));
$query = "INSERT INTO amazon_dropship (SKU, amazonSKU, Name,`Status`,Batch) VALUES ('$SKU', '$amazonSKU', '$Name', '$Status', '$Batch');";
//echo $query . "<br><Br>"; 
mysql_query($query) or die("Couldn't add row $query $SKU , $Replenish, $Name");
			}
	$row++;
}
fclose($fileHandle);

?>

<tr> 
<td width="100%" bgcolor="#FFE3BB" colspan="2"> 
<p style="margin:10px;"><font face="Verdana" size="2"> 
 The details of the uploaded file are shown below:&nbsp;</font></td>
</tr> 
<tr> 
<td width="100%" bgcolor="#FFE3BB" colspan="2"> 
<p style="margin-left: 10; margin-right: 10"><font face="Verdana" size="2"> 
 <?php echo "<b>File Name:</b> $fileUpload_name <br>"; ?> &nbsp;</font></td>
</tr>
<tr> 
<td width="100%" bgcolor="#FFE3BB" colspan="2"> 
<p style="margin-left: 10; margin-right: 10"><font face="Verdana" size="2"> 
 <?php echo "<b>File Type:</b> $fileUpload_type "; ?> <br>&nbsp;</font></td>
</tr>
<tr> 
<td width="100%" bgcolor="#FFE3BB" colspan="2"> 
<p style="margin-left: 10; margin-right: 10"><font face="Verdana" size="2"> 
 <?php echo "<b>File Size:</b> $fileUpload_size";  ?> <br>&nbsp;</font></td>
</tr>
<tr> 
<td width="100%" bgcolor="#FFE3BB" colspan="2"> 
<p style="margin-left: 10; margin-right: 10"><font face="Verdana" size="2"> 
 <?php echo "<b> Number of Products: $row <br> "; ?> &nbsp;</font></td>
</tr>
<tr> 
<td width="100%" bgcolor="#FFE3BB" colspan="2"> 
<p style="margin-left: 10; margin-right: 10"><font face="Verdana" size="2"> 
 <?php echo "<b> Number of Products with 2 Images: $count2images <br> "; ?> &nbsp;</font></td>
</tr>
<tr> 
<td width="100%" bgcolor="#FFE3BB" colspan="2"> 
<p style="margin:10px;"><font face="Verdana" size="2"> 
<br> <a href='amazon-index.php'>Back to Amazon Vendor Central Center...</a></font>
</td>
</tr> 

</table>
</body> 
</html>
