Jump to content

[SOLVED] error in csv upload...


roldahayes

Recommended Posts

hi there, me again,

 

please look at this code, it returns the "Either you did not upload any file, you uploaded an empty file, you exceeded our maximum upload size, or the file got corrupted during transfer!" error whenever i try to upload a csv.

 

as in my last post about the login script (which is now solved!) it all works on a number of different servers...

 

<?php
/* 	
	Wykes Online Products Database and Querying System

	filename: 		upl_file.php
 	version: 		1.1
	last revision:	23/11/02
	description:	upload file processing page

*/ 

// include the site header code
include_once("head.php");
// include the wykes website configuration file
include_once("web_conf.php");

// include the timer file
include_once("timer.php");

$startTime = getmicrotime();

// set page heading	
print "<img src=new/1_r1_c12.gif width=348 height=120><img src=new/1_r1_c5.gif width=350 height=120><br><br><font class=bigheader>Upload Summary</font><br><br>";

// check if no file or an empty file was uploaded
if ((($_POST['userfile']['error']) > 0) || (($_POST['userfile']['size']) == 0) )
{
	 echo "<p><font class=std>Either you did not upload any file, you uploaded an empty file, you exceeded our maximum upload size, or the file got corrupted during transfer!</font></p>";		
	 echo("<p><font class=std><a href=\"upload.php\">Click here to try upload again</a></font></p>");
	 echo("<p><font class=std><a href=\"view.php\">View Changes</a></font></p>");
	 return;
}

// use the admin_connection include file's connectDB function
include_once("adm_conn.php");
if(!connectDB())
{
	echo "<p><font class=std>Unable To Connect To Database</font></p>";
	echo("<BR><font class=std><a href=\"upload.php\">Click here to try upload again</a></font><BR>");
	echo("<BR><font class=std><a href=\"view.php?page=View Changes\">View Changes</a></font><BR>");
	return;
}

// set upload filename
$uploadFilename = "products.csv";	

// check file extension is .csv
$filename = ($_POST['userfile']['name']);
$fileEXT = explode ( '.', $filename );
$fileEXT = $fileEXT[count($fileEXT)-1];
//echo "<p>fileEXT = $fileEXT</p>"; //*debug
if (!($fileEXT == "csv"))
{
	echo("<p><font class=std><b>File is not a CSV</b></font><p>");
	echo("<p><font class=std><a href=\"upload.php\">Click here to try upload again</a></font></p>");
	echo("<p><font class=std><a href=\"view.php\">View Changes</a></font></p>");
	mysql_close();
	return;
}

    // check if file is bigger then MAXFILESIZE	
if ($_POST['userfile']['size'] > $MAXFILESIZE)
{
	echo("<font class=std>File Is Bigger Then " . $MAXFILESIZE . " bytes</font><BR>");
	echo("<BR><font class=std><a href=\"upload.php\">Click here to try upload again</a></font><BR>");
	echo("<BR><font class=std><a href=\"view.php\">View Changes</a></font><BR>");
	mysql_close();
	return;
}

// display upload file details
printf("<BR><font class=std><b>Upload File Details</b></font><br><br>");
printf("<font class=std>Name: %s <br>", $_POST['userfile']['name']);
    printf("<font class=std>Size: %s <br>", $_POST['userfile']['size']. " bytes<br></font>");

// move the temp file into the Upload directory
    if (move_uploaded_file($_POST["userfile"]["tmp_name"],$UPLOADDIR . $uploadFilename)) 
      printf("<font class=std><b>File Uploaded</b></font>");
    else 
{
        printf("<font class=std><b>Error: failed to load file</b></font>");
		echo("<font class=std><BR><a href=\"upload.php\">Click here to try upload again</a><BR></font>");
		echo("<font class=std><BR><a href=\"view.php\">View Changes</a><BR></font>");
		mysql_close();
		return;
}

// open a connection to the file
if (!($fp=fopen ($UPLOADDIR . $uploadFilename, "rb")))
{
	printf("<font class=std>Could not open " . $uploadFilename);
	unlink($UPLOADDIR . $uploadFilename);
	echo("<BR><font class=std><a href=\"upload.php\">Click here to try upload again</a></font><BR>");
	echo("<BR><font class=std><a href=\"view.php\">View Changes</a></font><BR>");
	mysql_close();
	return;
}
    
// set up rowData 2 dimensional array to read in data and a row index
$rowData[100][30];
$rowIndex = -1;
$index = 1;

// read in csv
while ($data = fgetcsv ($fp, 1000)) 
{			
	// miss out the first 2 rows (table header and blank row)
	if ($index > 2)
	{
		$rowIndex++;
		// if the first value(reference) is blank stop reading in, take 1 of rowIndex for rowcount
		if (($data[0] == NULL)) 
		{
			$rowIndex--;
			break;
		}
		// read in the 30 fields of the row
		for ($i=0; $i < 29; $i++) 
	  		$rowData[$rowIndex][$i] = $data[$i];
			//printf("rowData [" . $rowIndex . "][" . $i . "] = " . $rowData[$rowIndex][$c] . "<br>");  //*debug
	}
	$index++;
}

// close connection to file 
fclose ($fp);

// delete the uploaded file
unlink($UPLOADDIR . $uploadFilename);

// delete the current data from Products
$sqlquery = "DELETE FROM products";
$result = mysql_query($sqlquery);

if ($result == 0)
  	{
	echo("<B>Error " . mysql_errno() . ": " . mysql_error() . "</B>");
	mysql_close();
	return;
}

// start sql query string
	$sqlquery = "INSERT INTO products" . $fields . "VALUES ";

// add each row of the rowData array into a Multiple Values Insert Query (much faster then individual INSERT queries)
foreach($rowData as $sKey => $Row) 
{

	$sqlquery .= "('";

	// add each field of the row to the sql query
	foreach ($Row as $sField)
		$sqlquery .= addslashes($sField) . "', '";

	// get the sql query length and take off the last ", '" chars			
	$queryLength = strlen($sqlquery);
	$sqlquery = substr($sqlquery, 0, $queryLength - 3);
	$sqlquery .= "), ";
}

// get the sql query length and take off the last ", " chars			
$queryLength = strlen($sqlquery);
$sqlquery = substr($sqlquery, 0, $queryLength - 2);

//printf("<P><font class=std>" . $sqlquery . "</font></p>"); //*debug				

// run the query
$result = mysql_query($sqlquery);

if ($result == 0)
{
	echo("<B><font class=std>Error " . mysql_errno() . ": " . mysql_error() . "</font></B>");
	echo("<p><font class=std> Database not updated. Please re-attmempt your upload or contact the website support team</font></p>");
	echo("<BR><font class=std><a href=\"upload.php\">Click here to try upload again</a></font><BR>");
	mysql_close();
	return;
}	

// cleanup expired queries on the basket table from all users
//$sqlquery = "DELETE FROM BASKET WHERE expireTime < " . time();
//$result = mysql_query($sqlquery);

// close the wykes database connection
mysql_close();

// display upload information
echo("<BR><B><font class=std> " . ($rowIndex + 1) . " records in Database Updated</font></B><BR>");

$endTime = getmicrotime();

echo ("<BR><font class=std>Total excecution time to upload CSV and update Database =  " . number_format(($endTime - $startTime),3) . " seconds</font><BR>"); //*debug				
echo("<BR><font class=std><a href=\"upload.php\">Click here to try upload again</a></font><BR>");
echo("<BR><font class=std><a href=\"view.php\">View Changes</a></font><BR>");

return;
// include the site footer code
include_once("foot.php");
?>

Link to comment
https://forums.phpfreaks.com/topic/69952-solved-error-in-csv-upload/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.