Location: PHPKode > scripts > AGTC - CSV File Upload Script > agtc-csv-file-upload-script/index.php
 <?php
// *************************************************************************************************
// Title: 			PHP AGTC-CSV Upload Script v1.0a
// Developed by: 	Andy Greenhalgh
// Email:			hide@address.com
// Website:			agtc.co.uk
// Copyright:		2004-2005-2006(C)Andy Greenhalgh - (AGTC)
// Licence:			GPL, You may use this software under the terms of this General Public License
//					You must leave the copyrights intact. You cannot sell or distribute.
// *************************************************************************************************
//
error_reporting(E_ALL);
include_once "config.inc";
$php_self = $_SERVER['PHP_SELF'];
$file_open = 0;

$page = "<center>
<div align='center' style='border: 1px solid #000000; background-color: #000000;padding: 5px; color: #071bfd; width: 550px; font-family: verdana, arial, sans-serif;' >
<table align='center' style='border: 1px solid #eff7c2; background-color: #e2feea;padding: 10px; color: #071bfd;' cellspacing=5><tbody>
<tr><td style='background-color: #000000;padding: 5px'><u><b><h3><font color='#ffffff'><center>AGTC - CSV Upload Script v1.0a &copy;2006</center></font></h3></u></b></td></tr> 
<tr><td><span style=\"font-family: arial, sans-serif;\"><i><b>Instructions on use:</b></i> <br />
Before using this script please read the <a href='README.html'>README.html</a> file to check you have installed it correctly.<br />
Please make sure you are trying to upload a TXT or CSV file only with the extension .txt or .csv at the end of the file name. <br />
Do not exceed the maximum upload size of 2mb.<br />
You can create your files using MS-Excel or similar CSV creator.<br /><br /></td></tr>
<tr><td style='background-color: #000000;padding: 5px'>
<form enctype='multipart/form-data' action='$php_self' method='post' onSubmit='Alert()'>
<input type='hidden' name='MAX_FILE_SIZE' value='2000000' />
<input type='hidden' name='option' value='yes' /> 
<font color='#ffffff' size='-1'>Locate  CSV File: <br /></font><input name='csvfile' type='file' size='70' />&nbsp;<br />
<center><input type='submit' value='Upload CSV File' />
</td></tr></tbody></table></div></center>";


if(!isset($_POST['option'])){ 
			echo $page;
		}
		elseif($_POST['option'] == "yes"){
		$uploaddir = './uploads/';
		$uploadfile = $uploaddir . $_FILES['csvfile']['name'];
		if (move_uploaded_file($_FILES['csvfile']['tmp_name'], $uploadfile)) {
		print "<font face='arial'>Successfully uploaded. ";
		$notice = 1;
		chmod($uploadfile, 0777);
		} else {
		print "<font face='arial'>ERROR: Upload Failed. ";
		$notice = 0;
		} 
		$notice = 1;
		if ($notice == 1) {
		echo "\n sending Data...";
		flush();
		echo "</font>";
		if (file_exists($uploadfile)) {
		$fp = fopen($uploadfile, 'r') or die (" Cannot open the file");
		$fileopen = 1;
		$length = calculate_length($uploadfile);
		}
		$replace = "REPLACE";
		$field_terminater = ",";
		$enclose_option = 1;
		$enclosed = '"';
		$escaped = '\\\\';
		$line_terminator = 1;
		$local_option = 1;
		$sql_query     =  'LOAD DATA';
			if ($local_option == "1") {
				$sql_query     .= ' LOCAL';
			}
			$sql_query     .= ' INFILE \'' . $uploadfile . '\'';
			if (!empty($replace)) {
				$sql_query .= ' ' . $replace;
			}
			$sql_query     .= ' INTO TABLE ' . "`csv_test`";
			if (isset($field_terminater)) {
				$sql_query .= ' FIELDS TERMINATED BY \'' . $field_terminater . '\'';
			}
			if (isset($enclose_option) && strlen($enclose_option) > 0) {
				$sql_query .= ' OPTIONALLY';
			}
			if (strlen($enclosed) > 0) {
				$sql_query .= ' ENCLOSED BY \'' . $enclosed . '\'';
			}
			if (strlen($escaped) > 0) {
				$sql_query .= ' ESCAPED BY \'' . $escaped . '\'';
			}
			if (strlen($line_terminator) > 0){
				$sql_query .= ' LINES TERMINATED BY \'' . '\n' . '\'';
			}
		
		$result = mysql_query ($sql_query);
		echo mysql_error() ;
		
		if(mysql_affected_rows() > 1) {
				echo " <div align=left><b><font color='#071bfd' face='arial'>The CSV data has been added to your database.<br><br> <a href='javascript:history.back()'><<< Return to CSV Upload Script</a></font></div> ";
		}
		else {
			error_log(mysql_error());
			echo " <div align=left><b><font color='#e42127' face='arial' > ERROR, could not add the CSV data to your database.</font></div>";
		}
		
		if ($file_open ==1) {
		fclose($fp) or die("ERROR: Could not close the file");
		}
	}
}

function calculate_length($fp) {
   $length = 1000;
   $array = file($fp);
   for($i=0;$i<count($array);$i++)
   {
       if ($length < strlen($array[$i]))
       {
           $length = strlen($array[$i]);
       }
   }
   unset($array);
   return $length;
}
?>
<script language="Javascript">
 function Alert() {
var question = alert("Confirm this is a CSV file no more than 2mb in size!");
return question;
 }
 </script>
Return current item: AGTC - CSV File Upload Script